summaryrefslogtreecommitdiffstats
path: root/meta/packages/linux/linux-moblin-2.6.27-rc1/0008_drm.Add_GEM_graphics_execution_manager_to_i915_driver.patch
diff options
context:
space:
mode:
Diffstat (limited to 'meta/packages/linux/linux-moblin-2.6.27-rc1/0008_drm.Add_GEM_graphics_execution_manager_to_i915_driver.patch')
-rw-r--r--meta/packages/linux/linux-moblin-2.6.27-rc1/0008_drm.Add_GEM_graphics_execution_manager_to_i915_driver.patch5453
1 files changed, 5453 insertions, 0 deletions
diff --git a/meta/packages/linux/linux-moblin-2.6.27-rc1/0008_drm.Add_GEM_graphics_execution_manager_to_i915_driver.patch b/meta/packages/linux/linux-moblin-2.6.27-rc1/0008_drm.Add_GEM_graphics_execution_manager_to_i915_driver.patch
new file mode 100644
index 0000000000..e7ae851c4c
--- /dev/null
+++ b/meta/packages/linux/linux-moblin-2.6.27-rc1/0008_drm.Add_GEM_graphics_execution_manager_to_i915_driver.patch
@@ -0,0 +1,5453 @@
1From: Eric Anholt <eric@anholt.net>
2Date: Wed, 30 Jul 2008 19:06:12 +0000 (-0700)
3Subject: drm: Add GEM ("graphics execution manager") to i915 driver.
4X-Git-Tag: v2.6.12-rc2
5X-Git-Url: http://gitweb.freedesktop.org/?p=users/anholt/anholt/linux-2.6.git;a=commitdiff;h=drm-gem-merge
6
7drm: Add GEM ("graphics execution manager") to i915 driver.
8
9GEM allows the creation of persistent buffer objects accessible by the
10graphics device through new ioctls for managing execution of commands on the
11device. The userland API is almost entirely driver-specific to ensure that
12any driver building on this model can easily map the interface to individual
13driver requirements.
14
15GEM is used by the 2d driver for managing its internal state allocations and
16will be used for pixmap storage to reduce memory consumption and enable
17zero-copy GLX_EXT_texture_from_pixmap, and in the 3d driver is used to enable
18GL_EXT_framebuffer_object and GL_ARB_pixel_buffer_object.
19
20Signed-off-by: Eric Anholt <eric@anholt.net>
21---
22
23--- a/drivers/gpu/drm/Makefile
24+++ b/drivers/gpu/drm/Makefile
25@@ -4,8 +4,9 @@
26
27 ccflags-y := -Iinclude/drm
28
29-drm-y := drm_auth.o drm_bufs.o drm_context.o drm_dma.o drm_drawable.o \
30- drm_drv.o drm_fops.o drm_ioctl.o drm_irq.o \
31+drm-y := drm_auth.o drm_bufs.o drm_cache.o \
32+ drm_context.o drm_dma.o drm_drawable.o \
33+ drm_drv.o drm_fops.o drm_gem.o drm_ioctl.o drm_irq.o \
34 drm_lock.o drm_memory.o drm_proc.o drm_stub.o drm_vm.o \
35 drm_agpsupport.o drm_scatter.o ati_pcigart.o drm_pci.o \
36 drm_sysfs.o drm_hashtab.o drm_sman.o drm_mm.o
37--- a/drivers/gpu/drm/drm_agpsupport.c
38+++ b/drivers/gpu/drm/drm_agpsupport.c
39@@ -33,6 +33,7 @@
40
41 #include "drmP.h"
42 #include <linux/module.h>
43+#include <asm/agp.h>
44
45 #if __OS_HAS_AGP
46
47@@ -452,4 +453,52 @@ int drm_agp_unbind_memory(DRM_AGP_MEM *
48 return agp_unbind_memory(handle);
49 }
50
51-#endif /* __OS_HAS_AGP */
52+/**
53+ * Binds a collection of pages into AGP memory at the given offset, returning
54+ * the AGP memory structure containing them.
55+ *
56+ * No reference is held on the pages during this time -- it is up to the
57+ * caller to handle that.
58+ */
59+DRM_AGP_MEM *
60+drm_agp_bind_pages(struct drm_device *dev,
61+ struct page **pages,
62+ unsigned long num_pages,
63+ uint32_t gtt_offset)
64+{
65+ DRM_AGP_MEM *mem;
66+ int ret, i;
67+
68+ DRM_DEBUG("\n");
69+
70+ mem = drm_agp_allocate_memory(dev->agp->bridge, num_pages,
71+ AGP_USER_MEMORY);
72+ if (mem == NULL) {
73+ DRM_ERROR("Failed to allocate memory for %ld pages\n",
74+ num_pages);
75+ return NULL;
76+ }
77+
78+ for (i = 0; i < num_pages; i++)
79+ mem->memory[i] = phys_to_gart(page_to_phys(pages[i]));
80+ mem->page_count = num_pages;
81+
82+ mem->is_flushed = true;
83+ ret = drm_agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
84+ if (ret != 0) {
85+ DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
86+ agp_free_memory(mem);
87+ return NULL;
88+ }
89+
90+ return mem;
91+}
92+EXPORT_SYMBOL(drm_agp_bind_pages);
93+
94+void drm_agp_chipset_flush(struct drm_device *dev)
95+{
96+ agp_flush_chipset(dev->agp->bridge);
97+}
98+EXPORT_SYMBOL(drm_agp_chipset_flush);
99+
100+#endif /* __OS_HAS_AGP */
101--- /dev/null
102+++ b/drivers/gpu/drm/drm_cache.c
103@@ -0,0 +1,76 @@
104+/**************************************************************************
105+ *
106+ * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
107+ * All Rights Reserved.
108+ *
109+ * Permission is hereby granted, free of charge, to any person obtaining a
110+ * copy of this software and associated documentation files (the
111+ * "Software"), to deal in the Software without restriction, including
112+ * without limitation the rights to use, copy, modify, merge, publish,
113+ * distribute, sub license, and/or sell copies of the Software, and to
114+ * permit persons to whom the Software is furnished to do so, subject to
115+ * the following conditions:
116+ *
117+ * The above copyright notice and this permission notice (including the
118+ * next paragraph) shall be included in all copies or substantial portions
119+ * of the Software.
120+ *
121+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
122+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
123+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
124+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
125+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
126+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
127+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
128+ *
129+ **************************************************************************/
130+/*
131+ * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
132+ */
133+
134+#include "drmP.h"
135+
136+#if defined(CONFIG_X86)
137+static void
138+drm_clflush_page(struct page *page)
139+{
140+ uint8_t *page_virtual;
141+ unsigned int i;
142+
143+ if (unlikely(page == NULL))
144+ return;
145+
146+ page_virtual = kmap_atomic(page, KM_USER0);
147+ for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
148+ clflush(page_virtual + i);
149+ kunmap_atomic(page_virtual, KM_USER0);
150+}
151+#endif
152+
153+static void
154+drm_clflush_ipi_handler(void *null)
155+{
156+ wbinvd();
157+}
158+
159+void
160+drm_clflush_pages(struct page *pages[], unsigned long num_pages)
161+{
162+
163+#if defined(CONFIG_X86)
164+ if (cpu_has_clflush) {
165+ unsigned long i;
166+
167+ mb();
168+ for (i = 0; i < num_pages; ++i)
169+ drm_clflush_page(*pages++);
170+ mb();
171+
172+ return;
173+ }
174+#endif
175+
176+ if (on_each_cpu(drm_clflush_ipi_handler, NULL, 1) != 0)
177+ DRM_ERROR("Timed out waiting for cache flush.\n");
178+}
179+EXPORT_SYMBOL(drm_clflush_pages);
180--- a/drivers/gpu/drm/drm_drv.c
181+++ b/drivers/gpu/drm/drm_drv.c
182@@ -117,6 +117,10 @@ static struct drm_ioctl_desc drm_ioctls[
183 DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, 0),
184
185 DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_update_drawable_info, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
186+
187+ DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, 0),
188+ DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH),
189+ DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH),
190 };
191
192 #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )
193--- a/drivers/gpu/drm/drm_fops.c
194+++ b/drivers/gpu/drm/drm_fops.c
195@@ -256,6 +256,9 @@ static int drm_open_helper(struct inode
196
197 INIT_LIST_HEAD(&priv->lhead);
198
199+ if (dev->driver->driver_features & DRIVER_GEM)
200+ drm_gem_open(dev, priv);
201+
202 if (dev->driver->open) {
203 ret = dev->driver->open(dev, priv);
204 if (ret < 0)
205@@ -400,6 +403,9 @@ int drm_release(struct inode *inode, str
206 dev->driver->reclaim_buffers(dev, file_priv);
207 }
208
209+ if (dev->driver->driver_features & DRIVER_GEM)
210+ drm_gem_release(dev, file_priv);
211+
212 drm_fasync(-1, filp, 0);
213
214 mutex_lock(&dev->ctxlist_mutex);
215--- /dev/null
216+++ b/drivers/gpu/drm/drm_gem.c
217@@ -0,0 +1,420 @@
218+/*
219+ * Copyright © 2008 Intel Corporation
220+ *
221+ * Permission is hereby granted, free of charge, to any person obtaining a
222+ * copy of this software and associated documentation files (the "Software"),
223+ * to deal in the Software without restriction, including without limitation
224+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
225+ * and/or sell copies of the Software, and to permit persons to whom the
226+ * Software is furnished to do so, subject to the following conditions:
227+ *
228+ * The above copyright notice and this permission notice (including the next
229+ * paragraph) shall be included in all copies or substantial portions of the
230+ * Software.
231+ *
232+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
233+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
234+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
235+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
236+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
237+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
238+ * IN THE SOFTWARE.
239+ *
240+ * Authors:
241+ * Eric Anholt <eric@anholt.net>
242+ *
243+ */
244+
245+#include <linux/types.h>
246+#include <linux/slab.h>
247+#include <linux/mm.h>
248+#include <linux/uaccess.h>
249+#include <linux/fs.h>
250+#include <linux/file.h>
251+#include <linux/module.h>
252+#include <linux/mman.h>
253+#include <linux/pagemap.h>
254+#include "drmP.h"
255+
256+/** @file drm_gem.c
257+ *
258+ * This file provides some of the base ioctls and library routines for
259+ * the graphics memory manager implemented by each device driver.
260+ *
261+ * Because various devices have different requirements in terms of
262+ * synchronization and migration strategies, implementing that is left up to
263+ * the driver, and all that the general API provides should be generic --
264+ * allocating objects, reading/writing data with the cpu, freeing objects.
265+ * Even there, platform-dependent optimizations for reading/writing data with
266+ * the CPU mean we'll likely hook those out to driver-specific calls. However,
267+ * the DRI2 implementation wants to have at least allocate/mmap be generic.
268+ *
269+ * The goal was to have swap-backed object allocation managed through
270+ * struct file. However, file descriptors as handles to a struct file have
271+ * two major failings:
272+ * - Process limits prevent more than 1024 or so being used at a time by
273+ * default.
274+ * - Inability to allocate high fds will aggravate the X Server's select()
275+ * handling, and likely that of many GL client applications as well.
276+ *
277+ * This led to a plan of using our own integer IDs (called handles, following
278+ * DRM terminology) to mimic fds, and implement the fd syscalls we need as
279+ * ioctls. The objects themselves will still include the struct file so
280+ * that we can transition to fds if the required kernel infrastructure shows
281+ * up at a later date, and as our interface with shmfs for memory allocation.
282+ */
283+
284+/**
285+ * Initialize the GEM device fields
286+ */
287+
288+int
289+drm_gem_init(struct drm_device *dev)
290+{
291+ spin_lock_init(&dev->object_name_lock);
292+ idr_init(&dev->object_name_idr);
293+ atomic_set(&dev->object_count, 0);
294+ atomic_set(&dev->object_memory, 0);
295+ atomic_set(&dev->pin_count, 0);
296+ atomic_set(&dev->pin_memory, 0);
297+ atomic_set(&dev->gtt_count, 0);
298+ atomic_set(&dev->gtt_memory, 0);
299+ return 0;
300+}
301+
302+/**
303+ * Allocate a GEM object of the specified size with shmfs backing store
304+ */
305+struct drm_gem_object *
306+drm_gem_object_alloc(struct drm_device *dev, size_t size)
307+{
308+ struct drm_gem_object *obj;
309+
310+ BUG_ON((size & (PAGE_SIZE - 1)) != 0);
311+
312+ obj = kcalloc(1, sizeof(*obj), GFP_KERNEL);
313+
314+ obj->dev = dev;
315+ obj->filp = shmem_file_setup("drm mm object", size, 0);
316+ if (IS_ERR(obj->filp)) {
317+ kfree(obj);
318+ return NULL;
319+ }
320+
321+ kref_init(&obj->refcount);
322+ kref_init(&obj->handlecount);
323+ obj->size = size;
324+ if (dev->driver->gem_init_object != NULL &&
325+ dev->driver->gem_init_object(obj) != 0) {
326+ fput(obj->filp);
327+ kfree(obj);
328+ return NULL;
329+ }
330+ atomic_inc(&dev->object_count);
331+ atomic_add(obj->size, &dev->object_memory);
332+ return obj;
333+}
334+EXPORT_SYMBOL(drm_gem_object_alloc);
335+
336+/**
337+ * Removes the mapping from handle to filp for this object.
338+ */
339+static int
340+drm_gem_handle_delete(struct drm_file *filp, int handle)
341+{
342+ struct drm_device *dev;
343+ struct drm_gem_object *obj;
344+
345+ /* This is gross. The idr system doesn't let us try a delete and
346+ * return an error code. It just spews if you fail at deleting.
347+ * So, we have to grab a lock around finding the object and then
348+ * doing the delete on it and dropping the refcount, or the user
349+ * could race us to double-decrement the refcount and cause a
350+ * use-after-free later. Given the frequency of our handle lookups,
351+ * we may want to use ida for number allocation and a hash table
352+ * for the pointers, anyway.
353+ */
354+ spin_lock(&filp->table_lock);
355+
356+ /* Check if we currently have a reference on the object */
357+ obj = idr_find(&filp->object_idr, handle);
358+ if (obj == NULL) {
359+ spin_unlock(&filp->table_lock);
360+ return -EINVAL;
361+ }
362+ dev = obj->dev;
363+
364+ /* Release reference and decrement refcount. */
365+ idr_remove(&filp->object_idr, handle);
366+ spin_unlock(&filp->table_lock);
367+
368+ mutex_lock(&dev->struct_mutex);
369+ drm_gem_object_handle_unreference(obj);
370+ mutex_unlock(&dev->struct_mutex);
371+
372+ return 0;
373+}
374+
375+/**
376+ * Create a handle for this object. This adds a handle reference
377+ * to the object, which includes a regular reference count. Callers
378+ * will likely want to dereference the object afterwards.
379+ */
380+int
381+drm_gem_handle_create(struct drm_file *file_priv,
382+ struct drm_gem_object *obj,
383+ int *handlep)
384+{
385+ int ret;
386+
387+ /*
388+ * Get the user-visible handle using idr.
389+ */
390+again:
391+ /* ensure there is space available to allocate a handle */
392+ if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
393+ return -ENOMEM;
394+
395+ /* do the allocation under our spinlock */
396+ spin_lock(&file_priv->table_lock);
397+ ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep);
398+ spin_unlock(&file_priv->table_lock);
399+ if (ret == -EAGAIN)
400+ goto again;
401+
402+ if (ret != 0)
403+ return ret;
404+
405+ drm_gem_object_handle_reference(obj);
406+ return 0;
407+}
408+EXPORT_SYMBOL(drm_gem_handle_create);
409+
410+/** Returns a reference to the object named by the handle. */
411+struct drm_gem_object *
412+drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
413+ int handle)
414+{
415+ struct drm_gem_object *obj;
416+
417+ spin_lock(&filp->table_lock);
418+
419+ /* Check if we currently have a reference on the object */
420+ obj = idr_find(&filp->object_idr, handle);
421+ if (obj == NULL) {
422+ spin_unlock(&filp->table_lock);
423+ return NULL;
424+ }
425+
426+ drm_gem_object_reference(obj);
427+
428+ spin_unlock(&filp->table_lock);
429+
430+ return obj;
431+}
432+EXPORT_SYMBOL(drm_gem_object_lookup);
433+
434+/**
435+ * Releases the handle to an mm object.
436+ */
437+int
438+drm_gem_close_ioctl(struct drm_device *dev, void *data,
439+ struct drm_file *file_priv)
440+{
441+ struct drm_gem_close *args = data;
442+ int ret;
443+
444+ if (!(dev->driver->driver_features & DRIVER_GEM))
445+ return -ENODEV;
446+
447+ ret = drm_gem_handle_delete(file_priv, args->handle);
448+
449+ return ret;
450+}
451+
452+/**
453+ * Create a global name for an object, returning the name.
454+ *
455+ * Note that the name does not hold a reference; when the object
456+ * is freed, the name goes away.
457+ */
458+int
459+drm_gem_flink_ioctl(struct drm_device *dev, void *data,
460+ struct drm_file *file_priv)
461+{
462+ struct drm_gem_flink *args = data;
463+ struct drm_gem_object *obj;
464+ int ret;
465+
466+ if (!(dev->driver->driver_features & DRIVER_GEM))
467+ return -ENODEV;
468+
469+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
470+ if (obj == NULL)
471+ return -EINVAL;
472+
473+again:
474+ if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0)
475+ return -ENOMEM;
476+
477+ spin_lock(&dev->object_name_lock);
478+ if (obj->name) {
479+ spin_unlock(&dev->object_name_lock);
480+ return -EEXIST;
481+ }
482+ ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
483+ &obj->name);
484+ spin_unlock(&dev->object_name_lock);
485+ if (ret == -EAGAIN)
486+ goto again;
487+
488+ if (ret != 0) {
489+ mutex_lock(&dev->struct_mutex);
490+ drm_gem_object_unreference(obj);
491+ mutex_unlock(&dev->struct_mutex);
492+ return ret;
493+ }
494+
495+ /*
496+ * Leave the reference from the lookup around as the
497+ * name table now holds one
498+ */
499+ args->name = (uint64_t) obj->name;
500+
501+ return 0;
502+}
503+
504+/**
505+ * Open an object using the global name, returning a handle and the size.
506+ *
507+ * This handle (of course) holds a reference to the object, so the object
508+ * will not go away until the handle is deleted.
509+ */
510+int
511+drm_gem_open_ioctl(struct drm_device *dev, void *data,
512+ struct drm_file *file_priv)
513+{
514+ struct drm_gem_open *args = data;
515+ struct drm_gem_object *obj;
516+ int ret;
517+ int handle;
518+
519+ if (!(dev->driver->driver_features & DRIVER_GEM))
520+ return -ENODEV;
521+
522+ spin_lock(&dev->object_name_lock);
523+ obj = idr_find(&dev->object_name_idr, (int) args->name);
524+ if (obj)
525+ drm_gem_object_reference(obj);
526+ spin_unlock(&dev->object_name_lock);
527+ if (!obj)
528+ return -ENOENT;
529+
530+ ret = drm_gem_handle_create(file_priv, obj, &handle);
531+ mutex_lock(&dev->struct_mutex);
532+ drm_gem_object_unreference(obj);
533+ mutex_unlock(&dev->struct_mutex);
534+ if (ret)
535+ return ret;
536+
537+ args->handle = handle;
538+ args->size = obj->size;
539+
540+ return 0;
541+}
542+
543+/**
544+ * Called at device open time, sets up the structure for handling refcounting
545+ * of mm objects.
546+ */
547+void
548+drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
549+{
550+ idr_init(&file_private->object_idr);
551+ spin_lock_init(&file_private->table_lock);
552+}
553+
554+/**
555+ * Called at device close to release the file's
556+ * handle references on objects.
557+ */
558+static int
559+drm_gem_object_release_handle(int id, void *ptr, void *data)
560+{
561+ struct drm_gem_object *obj = ptr;
562+
563+ drm_gem_object_handle_unreference(obj);
564+
565+ return 0;
566+}
567+
568+/**
569+ * Called at close time when the filp is going away.
570+ *
571+ * Releases any remaining references on objects by this filp.
572+ */
573+void
574+drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
575+{
576+ mutex_lock(&dev->struct_mutex);
577+ idr_for_each(&file_private->object_idr,
578+ &drm_gem_object_release_handle, NULL);
579+
580+ idr_destroy(&file_private->object_idr);
581+ mutex_unlock(&dev->struct_mutex);
582+}
583+
584+/**
585+ * Called after the last reference to the object has been lost.
586+ *
587+ * Frees the object
588+ */
589+void
590+drm_gem_object_free(struct kref *kref)
591+{
592+ struct drm_gem_object *obj = (struct drm_gem_object *) kref;
593+ struct drm_device *dev = obj->dev;
594+
595+ BUG_ON(!mutex_is_locked(&dev->struct_mutex));
596+
597+ if (dev->driver->gem_free_object != NULL)
598+ dev->driver->gem_free_object(obj);
599+
600+ fput(obj->filp);
601+ atomic_dec(&dev->object_count);
602+ atomic_sub(obj->size, &dev->object_memory);
603+ kfree(obj);
604+}
605+EXPORT_SYMBOL(drm_gem_object_free);
606+
607+/**
608+ * Called after the last handle to the object has been closed
609+ *
610+ * Removes any name for the object. Note that this must be
611+ * called before drm_gem_object_free or we'll be touching
612+ * freed memory
613+ */
614+void
615+drm_gem_object_handle_free(struct kref *kref)
616+{
617+ struct drm_gem_object *obj = container_of(kref,
618+ struct drm_gem_object,
619+ handlecount);
620+ struct drm_device *dev = obj->dev;
621+
622+ /* Remove any name for this object */
623+ spin_lock(&dev->object_name_lock);
624+ if (obj->name) {
625+ idr_remove(&dev->object_name_idr, obj->name);
626+ spin_unlock(&dev->object_name_lock);
627+ /*
628+ * The object name held a reference to this object, drop
629+ * that now.
630+ */
631+ drm_gem_object_unreference(obj);
632+ } else
633+ spin_unlock(&dev->object_name_lock);
634+
635+}
636+EXPORT_SYMBOL(drm_gem_object_handle_free);
637+
638--- a/drivers/gpu/drm/drm_memory.c
639+++ b/drivers/gpu/drm/drm_memory.c
640@@ -133,6 +133,7 @@ int drm_free_agp(DRM_AGP_MEM * handle, i
641 {
642 return drm_agp_free_memory(handle) ? 0 : -EINVAL;
643 }
644+EXPORT_SYMBOL(drm_free_agp);
645
646 /** Wrapper around agp_bind_memory() */
647 int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
648@@ -145,6 +146,7 @@ int drm_unbind_agp(DRM_AGP_MEM * handle)
649 {
650 return drm_agp_unbind_memory(handle);
651 }
652+EXPORT_SYMBOL(drm_unbind_agp);
653
654 #else /* __OS_HAS_AGP */
655 static inline void *agp_remap(unsigned long offset, unsigned long size,
656--- a/drivers/gpu/drm/drm_mm.c
657+++ b/drivers/gpu/drm/drm_mm.c
658@@ -169,6 +169,7 @@ struct drm_mm_node *drm_mm_get_block(str
659
660 return child;
661 }
662+EXPORT_SYMBOL(drm_mm_get_block);
663
664 /*
665 * Put a block. Merge with the previous and / or next block if they are free.
666@@ -217,6 +218,7 @@ void drm_mm_put_block(struct drm_mm_node
667 drm_free(cur, sizeof(*cur), DRM_MEM_MM);
668 }
669 }
670+EXPORT_SYMBOL(drm_mm_put_block);
671
672 struct drm_mm_node *drm_mm_search_free(const struct drm_mm * mm,
673 unsigned long size,
674@@ -265,6 +267,7 @@ int drm_mm_clean(struct drm_mm * mm)
675
676 return (head->next->next == head);
677 }
678+EXPORT_SYMBOL(drm_mm_search_free);
679
680 int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
681 {
682@@ -273,7 +276,7 @@ int drm_mm_init(struct drm_mm * mm, unsi
683
684 return drm_mm_create_tail_node(mm, start, size);
685 }
686-
687+EXPORT_SYMBOL(drm_mm_init);
688
689 void drm_mm_takedown(struct drm_mm * mm)
690 {
691--- a/drivers/gpu/drm/drm_proc.c
692+++ b/drivers/gpu/drm/drm_proc.c
693@@ -49,6 +49,10 @@ static int drm_queues_info(char *buf, ch
694 int request, int *eof, void *data);
695 static int drm_bufs_info(char *buf, char **start, off_t offset,
696 int request, int *eof, void *data);
697+static int drm_gem_name_info(char *buf, char **start, off_t offset,
698+ int request, int *eof, void *data);
699+static int drm_gem_object_info(char *buf, char **start, off_t offset,
700+ int request, int *eof, void *data);
701 #if DRM_DEBUG_CODE
702 static int drm_vma_info(char *buf, char **start, off_t offset,
703 int request, int *eof, void *data);
704@@ -60,13 +64,16 @@ static int drm_vma_info(char *buf, char
705 static struct drm_proc_list {
706 const char *name; /**< file name */
707 int (*f) (char *, char **, off_t, int, int *, void *); /**< proc callback*/
708+ u32 driver_features; /**< Required driver features for this entry */
709 } drm_proc_list[] = {
710- {"name", drm_name_info},
711- {"mem", drm_mem_info},
712- {"vm", drm_vm_info},
713- {"clients", drm_clients_info},
714- {"queues", drm_queues_info},
715- {"bufs", drm_bufs_info},
716+ {"name", drm_name_info, 0},
717+ {"mem", drm_mem_info, 0},
718+ {"vm", drm_vm_info, 0},
719+ {"clients", drm_clients_info, 0},
720+ {"queues", drm_queues_info, 0},
721+ {"bufs", drm_bufs_info, 0},
722+ {"gem_names", drm_gem_name_info, DRIVER_GEM},
723+ {"gem_objects", drm_gem_object_info, DRIVER_GEM},
724 #if DRM_DEBUG_CODE
725 {"vma", drm_vma_info},
726 #endif
727@@ -90,8 +97,9 @@ static struct drm_proc_list {
728 int drm_proc_init(struct drm_minor *minor, int minor_id,
729 struct proc_dir_entry *root)
730 {
731+ struct drm_device *dev = minor->dev;
732 struct proc_dir_entry *ent;
733- int i, j;
734+ int i, j, ret;
735 char name[64];
736
737 sprintf(name, "%d", minor_id);
738@@ -102,23 +110,42 @@ int drm_proc_init(struct drm_minor *mino
739 }
740
741 for (i = 0; i < DRM_PROC_ENTRIES; i++) {
742+ u32 features = drm_proc_list[i].driver_features;
743+
744+ if (features != 0 &&
745+ (dev->driver->driver_features & features) != features)
746+ continue;
747+
748 ent = create_proc_entry(drm_proc_list[i].name,
749 S_IFREG | S_IRUGO, minor->dev_root);
750 if (!ent) {
751 DRM_ERROR("Cannot create /proc/dri/%s/%s\n",
752 name, drm_proc_list[i].name);
753- for (j = 0; j < i; j++)
754- remove_proc_entry(drm_proc_list[i].name,
755- minor->dev_root);
756- remove_proc_entry(name, root);
757- minor->dev_root = NULL;
758- return -1;
759+ ret = -1;
760+ goto fail;
761 }
762 ent->read_proc = drm_proc_list[i].f;
763 ent->data = minor;
764 }
765
766+ if (dev->driver->proc_init) {
767+ ret = dev->driver->proc_init(minor);
768+ if (ret) {
769+ DRM_ERROR("DRM: Driver failed to initialize "
770+ "/proc/dri.\n");
771+ goto fail;
772+ }
773+ }
774+
775 return 0;
776+ fail:
777+
778+ for (j = 0; j < i; j++)
779+ remove_proc_entry(drm_proc_list[i].name,
780+ minor->dev_root);
781+ remove_proc_entry(name, root);
782+ minor->dev_root = NULL;
783+ return ret;
784 }
785
786 /**
787@@ -133,12 +160,16 @@ int drm_proc_init(struct drm_minor *mino
788 */
789 int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root)
790 {
791+ struct drm_device *dev = minor->dev;
792 int i;
793 char name[64];
794
795 if (!root || !minor->dev_root)
796 return 0;
797
798+ if (dev->driver->proc_cleanup)
799+ dev->driver->proc_cleanup(minor);
800+
801 for (i = 0; i < DRM_PROC_ENTRIES; i++)
802 remove_proc_entry(drm_proc_list[i].name, minor->dev_root);
803 sprintf(name, "%d", minor->index);
804@@ -480,6 +511,84 @@ static int drm_clients_info(char *buf, c
805 return ret;
806 }
807
808+struct drm_gem_name_info_data {
809+ int len;
810+ char *buf;
811+ int eof;
812+};
813+
814+static int drm_gem_one_name_info(int id, void *ptr, void *data)
815+{
816+ struct drm_gem_object *obj = ptr;
817+ struct drm_gem_name_info_data *nid = data;
818+
819+ DRM_INFO("name %d size %d\n", obj->name, obj->size);
820+ if (nid->eof)
821+ return 0;
822+
823+ nid->len += sprintf(&nid->buf[nid->len],
824+ "%6d%9d%8d%9d\n",
825+ obj->name, obj->size,
826+ atomic_read(&obj->handlecount.refcount),
827+ atomic_read(&obj->refcount.refcount));
828+ if (nid->len > DRM_PROC_LIMIT) {
829+ nid->eof = 1;
830+ return 0;
831+ }
832+ return 0;
833+}
834+
835+static int drm_gem_name_info(char *buf, char **start, off_t offset,
836+ int request, int *eof, void *data)
837+{
838+ struct drm_minor *minor = (struct drm_minor *) data;
839+ struct drm_device *dev = minor->dev;
840+ struct drm_gem_name_info_data nid;
841+
842+ if (offset > DRM_PROC_LIMIT) {
843+ *eof = 1;
844+ return 0;
845+ }
846+
847+ nid.len = sprintf(buf, " name size handles refcount\n");
848+ nid.buf = buf;
849+ nid.eof = 0;
850+ idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, &nid);
851+
852+ *start = &buf[offset];
853+ *eof = 0;
854+ if (nid.len > request + offset)
855+ return request;
856+ *eof = 1;
857+ return nid.len - offset;
858+}
859+
860+static int drm_gem_object_info(char *buf, char **start, off_t offset,
861+ int request, int *eof, void *data)
862+{
863+ struct drm_minor *minor = (struct drm_minor *) data;
864+ struct drm_device *dev = minor->dev;
865+ int len = 0;
866+
867+ if (offset > DRM_PROC_LIMIT) {
868+ *eof = 1;
869+ return 0;
870+ }
871+
872+ *start = &buf[offset];
873+ *eof = 0;
874+ DRM_PROC_PRINT("%d objects\n", atomic_read(&dev->object_count));
875+ DRM_PROC_PRINT("%d object bytes\n", atomic_read(&dev->object_memory));
876+ DRM_PROC_PRINT("%d pinned\n", atomic_read(&dev->pin_count));
877+ DRM_PROC_PRINT("%d pin bytes\n", atomic_read(&dev->pin_memory));
878+ DRM_PROC_PRINT("%d gtt bytes\n", atomic_read(&dev->gtt_memory));
879+ DRM_PROC_PRINT("%d gtt total\n", dev->gtt_total);
880+ if (len > request + offset)
881+ return request;
882+ *eof = 1;
883+ return len - offset;
884+}
885+
886 #if DRM_DEBUG_CODE
887
888 static int drm__vma_info(char *buf, char **start, off_t offset, int request,
889--- a/drivers/gpu/drm/drm_stub.c
890+++ b/drivers/gpu/drm/drm_stub.c
891@@ -152,6 +152,15 @@ static int drm_fill_in_dev(struct drm_de
892 goto error_out_unreg;
893 }
894
895+ if (driver->driver_features & DRIVER_GEM) {
896+ retcode = drm_gem_init(dev);
897+ if (retcode) {
898+ DRM_ERROR("Cannot initialize graphics execution "
899+ "manager (GEM)\n");
900+ goto error_out_unreg;
901+ }
902+ }
903+
904 return 0;
905
906 error_out_unreg:
907@@ -317,6 +326,7 @@ int drm_put_dev(struct drm_device * dev)
908 int drm_put_minor(struct drm_minor **minor_p)
909 {
910 struct drm_minor *minor = *minor_p;
911+
912 DRM_DEBUG("release secondary minor %d\n", minor->index);
913
914 if (minor->type == DRM_MINOR_LEGACY)
915--- a/drivers/gpu/drm/i915/Makefile
916+++ b/drivers/gpu/drm/i915/Makefile
917@@ -3,7 +3,11 @@
918 # Direct Rendering Infrastructure (DRI) in XFree86 4.1.0 and higher.
919
920 ccflags-y := -Iinclude/drm
921-i915-y := i915_drv.o i915_dma.o i915_irq.o i915_mem.o
922+i915-y := i915_drv.o i915_dma.o i915_irq.o i915_mem.o \
923+ i915_gem.o \
924+ i915_gem_debug.o \
925+ i915_gem_proc.o \
926+ i915_gem_tiling.o
927
928 i915-$(CONFIG_COMPAT) += i915_ioc32.o
929
930--- a/drivers/gpu/drm/i915/i915_dma.c
931+++ b/drivers/gpu/drm/i915/i915_dma.c
932@@ -170,24 +170,31 @@ static int i915_initialize(struct drm_de
933 dev_priv->sarea_priv = (drm_i915_sarea_t *)
934 ((u8 *) dev_priv->sarea->handle + init->sarea_priv_offset);
935
936- dev_priv->ring.Start = init->ring_start;
937- dev_priv->ring.End = init->ring_end;
938- dev_priv->ring.Size = init->ring_size;
939- dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
940-
941- dev_priv->ring.map.offset = init->ring_start;
942- dev_priv->ring.map.size = init->ring_size;
943- dev_priv->ring.map.type = 0;
944- dev_priv->ring.map.flags = 0;
945- dev_priv->ring.map.mtrr = 0;
946+ if (init->ring_size != 0) {
947+ if (dev_priv->ring.ring_obj != NULL) {
948+ i915_dma_cleanup(dev);
949+ DRM_ERROR("Client tried to initialize ringbuffer in "
950+ "GEM mode\n");
951+ return -EINVAL;
952+ }
953
954- drm_core_ioremap(&dev_priv->ring.map, dev);
955+ dev_priv->ring.Size = init->ring_size;
956+ dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
957
958- if (dev_priv->ring.map.handle == NULL) {
959- i915_dma_cleanup(dev);
960- DRM_ERROR("can not ioremap virtual address for"
961- " ring buffer\n");
962- return -ENOMEM;
963+ dev_priv->ring.map.offset = init->ring_start;
964+ dev_priv->ring.map.size = init->ring_size;
965+ dev_priv->ring.map.type = 0;
966+ dev_priv->ring.map.flags = 0;
967+ dev_priv->ring.map.mtrr = 0;
968+
969+ drm_core_ioremap(&dev_priv->ring.map, dev);
970+
971+ if (dev_priv->ring.map.handle == NULL) {
972+ i915_dma_cleanup(dev);
973+ DRM_ERROR("can not ioremap virtual address for"
974+ " ring buffer\n");
975+ return -ENOMEM;
976+ }
977 }
978
979 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
980@@ -377,9 +384,10 @@ static int i915_emit_cmds(struct drm_dev
981 return 0;
982 }
983
984-static int i915_emit_box(struct drm_device * dev,
985- struct drm_clip_rect __user * boxes,
986- int i, int DR1, int DR4)
987+int
988+i915_emit_box(struct drm_device *dev,
989+ struct drm_clip_rect __user *boxes,
990+ int i, int DR1, int DR4)
991 {
992 drm_i915_private_t *dev_priv = dev->dev_private;
993 struct drm_clip_rect box;
994@@ -681,6 +689,9 @@ static int i915_getparam(struct drm_devi
995 case I915_PARAM_LAST_DISPATCH:
996 value = READ_BREADCRUMB(dev_priv);
997 break;
998+ case I915_PARAM_HAS_GEM:
999+ value = 1;
1000+ break;
1001 default:
1002 DRM_ERROR("Unknown parameter %d\n", param->param);
1003 return -EINVAL;
1004@@ -784,6 +795,7 @@ int i915_driver_load(struct drm_device *
1005 memset(dev_priv, 0, sizeof(drm_i915_private_t));
1006
1007 dev->dev_private = (void *)dev_priv;
1008+ dev_priv->dev = dev;
1009
1010 /* Add register map (needed for suspend/resume) */
1011 base = drm_get_resource_start(dev, mmio_bar);
1012@@ -793,6 +805,8 @@ int i915_driver_load(struct drm_device *
1013 _DRM_KERNEL | _DRM_DRIVER,
1014 &dev_priv->mmio_map);
1015
1016+ i915_gem_load(dev);
1017+
1018 /* Init HWS */
1019 if (!I915_NEED_GFX_HWS(dev)) {
1020 ret = i915_init_phys_hws(dev);
1021@@ -833,6 +847,25 @@ int i915_driver_unload(struct drm_device
1022 return 0;
1023 }
1024
1025+int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1026+{
1027+ struct drm_i915_file_private *i915_file_priv;
1028+
1029+ DRM_DEBUG("\n");
1030+ i915_file_priv = (struct drm_i915_file_private *)
1031+ drm_alloc(sizeof(*i915_file_priv), DRM_MEM_FILES);
1032+
1033+ if (!i915_file_priv)
1034+ return -ENOMEM;
1035+
1036+ file_priv->driver_priv = i915_file_priv;
1037+
1038+ i915_file_priv->mm.last_gem_seqno = 0;
1039+ i915_file_priv->mm.last_gem_throttle_seqno = 0;
1040+
1041+ return 0;
1042+}
1043+
1044 void i915_driver_lastclose(struct drm_device * dev)
1045 {
1046 drm_i915_private_t *dev_priv = dev->dev_private;
1047@@ -840,6 +873,8 @@ void i915_driver_lastclose(struct drm_de
1048 if (!dev_priv)
1049 return;
1050
1051+ i915_gem_lastclose(dev);
1052+
1053 if (dev_priv->agp_heap)
1054 i915_mem_takedown(&(dev_priv->agp_heap));
1055
1056@@ -852,6 +887,13 @@ void i915_driver_preclose(struct drm_dev
1057 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
1058 }
1059
1060+void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1061+{
1062+ struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1063+
1064+ drm_free(i915_file_priv, sizeof(*i915_file_priv), DRM_MEM_FILES);
1065+}
1066+
1067 struct drm_ioctl_desc i915_ioctls[] = {
1068 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1069 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1070@@ -870,6 +912,22 @@ struct drm_ioctl_desc i915_ioctls[] = {
1071 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1072 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1073 DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH),
1074+ DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH),
1075+ DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
1076+ DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1077+ DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1078+ DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
1079+ DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
1080+ DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH),
1081+ DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH),
1082+ DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1083+ DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
1084+ DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
1085+ DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
1086+ DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
1087+ DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
1088+ DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1089+ DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
1090 };
1091
1092 int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
1093--- a/drivers/gpu/drm/i915/i915_drv.c
1094+++ b/drivers/gpu/drm/i915/i915_drv.c
1095@@ -542,11 +542,13 @@ static struct drm_driver driver = {
1096 .driver_features =
1097 DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | /* DRIVER_USE_MTRR |*/
1098 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_IRQ_VBL |
1099- DRIVER_IRQ_VBL2,
1100+ DRIVER_IRQ_VBL2 | DRIVER_GEM,
1101 .load = i915_driver_load,
1102 .unload = i915_driver_unload,
1103+ .open = i915_driver_open,
1104 .lastclose = i915_driver_lastclose,
1105 .preclose = i915_driver_preclose,
1106+ .postclose = i915_driver_postclose,
1107 .suspend = i915_suspend,
1108 .resume = i915_resume,
1109 .device_is_agp = i915_driver_device_is_agp,
1110@@ -559,6 +561,10 @@ static struct drm_driver driver = {
1111 .reclaim_buffers = drm_core_reclaim_buffers,
1112 .get_map_ofs = drm_core_get_map_ofs,
1113 .get_reg_ofs = drm_core_get_reg_ofs,
1114+ .proc_init = i915_gem_proc_init,
1115+ .proc_cleanup = i915_gem_proc_cleanup,
1116+ .gem_init_object = i915_gem_init_object,
1117+ .gem_free_object = i915_gem_free_object,
1118 .ioctls = i915_ioctls,
1119 .fops = {
1120 .owner = THIS_MODULE,
1121--- a/drivers/gpu/drm/i915/i915_drv.h
1122+++ b/drivers/gpu/drm/i915/i915_drv.h
1123@@ -39,7 +39,7 @@
1124
1125 #define DRIVER_NAME "i915"
1126 #define DRIVER_DESC "Intel Graphics"
1127-#define DRIVER_DATE "20060119"
1128+#define DRIVER_DATE "20080730"
1129
1130 /* Interface history:
1131 *
1132@@ -55,16 +55,23 @@
1133 #define DRIVER_MINOR 6
1134 #define DRIVER_PATCHLEVEL 0
1135
1136+#define WATCH_COHERENCY 0
1137+#define WATCH_BUF 0
1138+#define WATCH_EXEC 0
1139+#define WATCH_LRU 0
1140+#define WATCH_RELOC 0
1141+#define WATCH_INACTIVE 0
1142+#define WATCH_PWRITE 0
1143+
1144 typedef struct _drm_i915_ring_buffer {
1145 int tail_mask;
1146- unsigned long Start;
1147- unsigned long End;
1148 unsigned long Size;
1149 u8 *virtual_start;
1150 int head;
1151 int tail;
1152 int space;
1153 drm_local_map_t map;
1154+ struct drm_gem_object *ring_obj;
1155 } drm_i915_ring_buffer_t;
1156
1157 struct mem_block {
1158@@ -83,6 +90,8 @@ typedef struct _drm_i915_vbl_swap {
1159 } drm_i915_vbl_swap_t;
1160
1161 typedef struct drm_i915_private {
1162+ struct drm_device *dev;
1163+
1164 drm_local_map_t *sarea;
1165 drm_local_map_t *mmio_map;
1166
1167@@ -95,6 +104,7 @@ typedef struct drm_i915_private {
1168 unsigned long counter;
1169 unsigned int status_gfx_addr;
1170 drm_local_map_t hws_map;
1171+ struct drm_gem_object *hws_obj;
1172
1173 unsigned int cpp;
1174 int back_offset;
1175@@ -104,7 +114,6 @@ typedef struct drm_i915_private {
1176
1177 wait_queue_head_t irq_queue;
1178 atomic_t irq_received;
1179- atomic_t irq_emitted;
1180 /** Protects user_irq_refcount and irq_mask_reg */
1181 spinlock_t user_irq_lock;
1182 /** Refcount for i915_user_irq_get() versus i915_user_irq_put(). */
1183@@ -210,8 +219,174 @@ typedef struct drm_i915_private {
1184 u8 saveDACMASK;
1185 u8 saveDACDATA[256*3]; /* 256 3-byte colors */
1186 u8 saveCR[37];
1187+
1188+ struct {
1189+ struct drm_mm gtt_space;
1190+
1191+ /**
1192+ * List of objects currently involved in rendering from the
1193+ * ringbuffer.
1194+ *
1195+ * A reference is held on the buffer while on this list.
1196+ */
1197+ struct list_head active_list;
1198+
1199+ /**
1200+ * List of objects which are not in the ringbuffer but which
1201+ * still have a write_domain which needs to be flushed before
1202+ * unbinding.
1203+ *
1204+ * A reference is held on the buffer while on this list.
1205+ */
1206+ struct list_head flushing_list;
1207+
1208+ /**
1209+ * LRU list of objects which are not in the ringbuffer and
1210+ * are ready to unbind, but are still in the GTT.
1211+ *
1212+ * A reference is not held on the buffer while on this list,
1213+ * as merely being GTT-bound shouldn't prevent its being
1214+ * freed, and we'll pull it off the list in the free path.
1215+ */
1216+ struct list_head inactive_list;
1217+
1218+ /**
1219+ * List of breadcrumbs associated with GPU requests currently
1220+ * outstanding.
1221+ */
1222+ struct list_head request_list;
1223+
1224+ /**
1225+ * We leave the user IRQ off as much as possible,
1226+ * but this means that requests will finish and never
1227+ * be retired once the system goes idle. Set a timer to
1228+ * fire periodically while the ring is running. When it
1229+ * fires, go retire requests.
1230+ */
1231+ struct delayed_work retire_work;
1232+
1233+ uint32_t next_gem_seqno;
1234+
1235+ /**
1236+ * Waiting sequence number, if any
1237+ */
1238+ uint32_t waiting_gem_seqno;
1239+
1240+ /**
1241+ * Last seq seen at irq time
1242+ */
1243+ uint32_t irq_gem_seqno;
1244+
1245+ /**
1246+ * Flag if the X Server, and thus DRM, is not currently in
1247+ * control of the device.
1248+ *
1249+ * This is set between LeaveVT and EnterVT. It needs to be
1250+ * replaced with a semaphore. It also needs to be
1251+ * transitioned away from for kernel modesetting.
1252+ */
1253+ int suspended;
1254+
1255+ /**
1256+ * Flag if the hardware appears to be wedged.
1257+ *
1258+ * This is set when attempts to idle the device timeout.
1259+ * It prevents command submission from occuring and makes
1260+ * every pending request fail
1261+ */
1262+ int wedged;
1263+
1264+ /** Bit 6 swizzling required for X tiling */
1265+ uint32_t bit_6_swizzle_x;
1266+ /** Bit 6 swizzling required for Y tiling */
1267+ uint32_t bit_6_swizzle_y;
1268+ } mm;
1269 } drm_i915_private_t;
1270
1271+/** driver private structure attached to each drm_gem_object */
1272+struct drm_i915_gem_object {
1273+ struct drm_gem_object *obj;
1274+
1275+ /** Current space allocated to this object in the GTT, if any. */
1276+ struct drm_mm_node *gtt_space;
1277+
1278+ /** This object's place on the active/flushing/inactive lists */
1279+ struct list_head list;
1280+
1281+ /**
1282+ * This is set if the object is on the active or flushing lists
1283+ * (has pending rendering), and is not set if it's on inactive (ready
1284+ * to be unbound).
1285+ */
1286+ int active;
1287+
1288+ /**
1289+ * This is set if the object has been written to since last bound
1290+ * to the GTT
1291+ */
1292+ int dirty;
1293+
1294+ /** AGP memory structure for our GTT binding. */
1295+ DRM_AGP_MEM *agp_mem;
1296+
1297+ struct page **page_list;
1298+
1299+ /**
1300+ * Current offset of the object in GTT space.
1301+ *
1302+ * This is the same as gtt_space->start
1303+ */
1304+ uint32_t gtt_offset;
1305+
1306+ /** Boolean whether this object has a valid gtt offset. */
1307+ int gtt_bound;
1308+
1309+ /** How many users have pinned this object in GTT space */
1310+ int pin_count;
1311+
1312+ /** Breadcrumb of last rendering to the buffer. */
1313+ uint32_t last_rendering_seqno;
1314+
1315+ /** Current tiling mode for the object. */
1316+ uint32_t tiling_mode;
1317+
1318+ /**
1319+ * Flagging of which individual pages are valid in GEM_DOMAIN_CPU when
1320+ * GEM_DOMAIN_CPU is not in the object's read domain.
1321+ */
1322+ uint8_t *page_cpu_valid;
1323+};
1324+
1325+/**
1326+ * Request queue structure.
1327+ *
1328+ * The request queue allows us to note sequence numbers that have been emitted
1329+ * and may be associated with active buffers to be retired.
1330+ *
1331+ * By keeping this list, we can avoid having to do questionable
1332+ * sequence-number comparisons on buffer last_rendering_seqnos, and associate
1333+ * an emission time with seqnos for tracking how far ahead of the GPU we are.
1334+ */
1335+struct drm_i915_gem_request {
1336+ /** GEM sequence number associated with this request. */
1337+ uint32_t seqno;
1338+
1339+ /** Time at which this request was emitted, in jiffies. */
1340+ unsigned long emitted_jiffies;
1341+
1342+ /** Cache domains that were flushed at the start of the request. */
1343+ uint32_t flush_domains;
1344+
1345+ struct list_head list;
1346+};
1347+
1348+struct drm_i915_file_private {
1349+ struct {
1350+ uint32_t last_gem_seqno;
1351+ uint32_t last_gem_throttle_seqno;
1352+ } mm;
1353+};
1354+
1355 extern struct drm_ioctl_desc i915_ioctls[];
1356 extern int i915_max_ioctl;
1357
1358@@ -219,18 +394,26 @@ extern int i915_max_ioctl;
1359 extern void i915_kernel_lost_context(struct drm_device * dev);
1360 extern int i915_driver_load(struct drm_device *, unsigned long flags);
1361 extern int i915_driver_unload(struct drm_device *);
1362+extern int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv);
1363 extern void i915_driver_lastclose(struct drm_device * dev);
1364 extern void i915_driver_preclose(struct drm_device *dev,
1365 struct drm_file *file_priv);
1366+extern void i915_driver_postclose(struct drm_device *dev,
1367+ struct drm_file *file_priv);
1368 extern int i915_driver_device_is_agp(struct drm_device * dev);
1369 extern long i915_compat_ioctl(struct file *filp, unsigned int cmd,
1370 unsigned long arg);
1371+extern int i915_emit_box(struct drm_device *dev,
1372+ struct drm_clip_rect __user *boxes,
1373+ int i, int DR1, int DR4);
1374
1375 /* i915_irq.c */
1376 extern int i915_irq_emit(struct drm_device *dev, void *data,
1377 struct drm_file *file_priv);
1378 extern int i915_irq_wait(struct drm_device *dev, void *data,
1379 struct drm_file *file_priv);
1380+void i915_user_irq_get(struct drm_device *dev);
1381+void i915_user_irq_put(struct drm_device *dev);
1382
1383 extern int i915_driver_vblank_wait(struct drm_device *dev, unsigned int *sequence);
1384 extern int i915_driver_vblank_wait2(struct drm_device *dev, unsigned int *sequence);
1385@@ -257,6 +440,67 @@ extern int i915_mem_destroy_heap(struct
1386 extern void i915_mem_takedown(struct mem_block **heap);
1387 extern void i915_mem_release(struct drm_device * dev,
1388 struct drm_file *file_priv, struct mem_block *heap);
1389+/* i915_gem.c */
1390+int i915_gem_init_ioctl(struct drm_device *dev, void *data,
1391+ struct drm_file *file_priv);
1392+int i915_gem_create_ioctl(struct drm_device *dev, void *data,
1393+ struct drm_file *file_priv);
1394+int i915_gem_pread_ioctl(struct drm_device *dev, void *data,
1395+ struct drm_file *file_priv);
1396+int i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1397+ struct drm_file *file_priv);
1398+int i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1399+ struct drm_file *file_priv);
1400+int i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1401+ struct drm_file *file_priv);
1402+int i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1403+ struct drm_file *file_priv);
1404+int i915_gem_execbuffer(struct drm_device *dev, void *data,
1405+ struct drm_file *file_priv);
1406+int i915_gem_pin_ioctl(struct drm_device *dev, void *data,
1407+ struct drm_file *file_priv);
1408+int i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
1409+ struct drm_file *file_priv);
1410+int i915_gem_busy_ioctl(struct drm_device *dev, void *data,
1411+ struct drm_file *file_priv);
1412+int i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
1413+ struct drm_file *file_priv);
1414+int i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
1415+ struct drm_file *file_priv);
1416+int i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
1417+ struct drm_file *file_priv);
1418+int i915_gem_set_tiling(struct drm_device *dev, void *data,
1419+ struct drm_file *file_priv);
1420+int i915_gem_get_tiling(struct drm_device *dev, void *data,
1421+ struct drm_file *file_priv);
1422+void i915_gem_load(struct drm_device *dev);
1423+int i915_gem_proc_init(struct drm_minor *minor);
1424+void i915_gem_proc_cleanup(struct drm_minor *minor);
1425+int i915_gem_init_object(struct drm_gem_object *obj);
1426+void i915_gem_free_object(struct drm_gem_object *obj);
1427+int i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment);
1428+void i915_gem_object_unpin(struct drm_gem_object *obj);
1429+void i915_gem_lastclose(struct drm_device *dev);
1430+uint32_t i915_get_gem_seqno(struct drm_device *dev);
1431+void i915_gem_retire_requests(struct drm_device *dev);
1432+void i915_gem_retire_work_handler(struct work_struct *work);
1433+void i915_gem_clflush_object(struct drm_gem_object *obj);
1434+
1435+/* i915_gem_tiling.c */
1436+void i915_gem_detect_bit_6_swizzle(struct drm_device *dev);
1437+
1438+/* i915_gem_debug.c */
1439+void i915_gem_dump_object(struct drm_gem_object *obj, int len,
1440+ const char *where, uint32_t mark);
1441+#if WATCH_INACTIVE
1442+void i915_verify_inactive(struct drm_device *dev, char *file, int line);
1443+#else
1444+#define i915_verify_inactive(dev, file, line)
1445+#endif
1446+void i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle);
1447+void i915_gem_dump_object(struct drm_gem_object *obj, int len,
1448+ const char *where, uint32_t mark);
1449+void i915_dump_lru(struct drm_device *dev, const char *where);
1450
1451 #define I915_READ(reg) DRM_READ32(dev_priv->mmio_map, (reg))
1452 #define I915_WRITE(reg,val) DRM_WRITE32(dev_priv->mmio_map, (reg), (val))
1453@@ -309,6 +553,7 @@ extern void i915_mem_release(struct drm_
1454 */
1455 #define READ_HWSP(dev_priv, reg) (((volatile u32*)(dev_priv->hw_status_page))[reg])
1456 #define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, 5)
1457+#define I915_GEM_HWS_INDEX 0x10
1458
1459 extern int i915_wait_ring(struct drm_device * dev, int n, const char *caller);
1460
1461--- /dev/null
1462+++ b/drivers/gpu/drm/i915/i915_gem.c
1463@@ -0,0 +1,2508 @@
1464+/*
1465+ * Copyright © 2008 Intel Corporation
1466+ *
1467+ * Permission is hereby granted, free of charge, to any person obtaining a
1468+ * copy of this software and associated documentation files (the "Software"),
1469+ * to deal in the Software without restriction, including without limitation
1470+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1471+ * and/or sell copies of the Software, and to permit persons to whom the
1472+ * Software is furnished to do so, subject to the following conditions:
1473+ *
1474+ * The above copyright notice and this permission notice (including the next
1475+ * paragraph) shall be included in all copies or substantial portions of the
1476+ * Software.
1477+ *
1478+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1479+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1480+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1481+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1482+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1483+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1484+ * IN THE SOFTWARE.
1485+ *
1486+ * Authors:
1487+ * Eric Anholt <eric@anholt.net>
1488+ *
1489+ */
1490+
1491+#include "drmP.h"
1492+#include "drm.h"
1493+#include "i915_drm.h"
1494+#include "i915_drv.h"
1495+#include <linux/swap.h>
1496+
1497+static int
1498+i915_gem_object_set_domain(struct drm_gem_object *obj,
1499+ uint32_t read_domains,
1500+ uint32_t write_domain);
1501+static int
1502+i915_gem_object_set_domain_range(struct drm_gem_object *obj,
1503+ uint64_t offset,
1504+ uint64_t size,
1505+ uint32_t read_domains,
1506+ uint32_t write_domain);
1507+static int
1508+i915_gem_set_domain(struct drm_gem_object *obj,
1509+ struct drm_file *file_priv,
1510+ uint32_t read_domains,
1511+ uint32_t write_domain);
1512+static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
1513+static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
1514+static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
1515+
1516+int
1517+i915_gem_init_ioctl(struct drm_device *dev, void *data,
1518+ struct drm_file *file_priv)
1519+{
1520+ drm_i915_private_t *dev_priv = dev->dev_private;
1521+ struct drm_i915_gem_init *args = data;
1522+
1523+ mutex_lock(&dev->struct_mutex);
1524+
1525+ if (args->gtt_start >= args->gtt_end ||
1526+ (args->gtt_start & (PAGE_SIZE - 1)) != 0 ||
1527+ (args->gtt_end & (PAGE_SIZE - 1)) != 0) {
1528+ mutex_unlock(&dev->struct_mutex);
1529+ return -EINVAL;
1530+ }
1531+
1532+ drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start,
1533+ args->gtt_end - args->gtt_start);
1534+
1535+ dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start);
1536+
1537+ mutex_unlock(&dev->struct_mutex);
1538+
1539+ return 0;
1540+}
1541+
1542+
1543+/**
1544+ * Creates a new mm object and returns a handle to it.
1545+ */
1546+int
1547+i915_gem_create_ioctl(struct drm_device *dev, void *data,
1548+ struct drm_file *file_priv)
1549+{
1550+ struct drm_i915_gem_create *args = data;
1551+ struct drm_gem_object *obj;
1552+ int handle, ret;
1553+
1554+ args->size = roundup(args->size, PAGE_SIZE);
1555+
1556+ /* Allocate the new object */
1557+ obj = drm_gem_object_alloc(dev, args->size);
1558+ if (obj == NULL)
1559+ return -ENOMEM;
1560+
1561+ ret = drm_gem_handle_create(file_priv, obj, &handle);
1562+ mutex_lock(&dev->struct_mutex);
1563+ drm_gem_object_handle_unreference(obj);
1564+ mutex_unlock(&dev->struct_mutex);
1565+
1566+ if (ret)
1567+ return ret;
1568+
1569+ args->handle = handle;
1570+
1571+ return 0;
1572+}
1573+
1574+/**
1575+ * Reads data from the object referenced by handle.
1576+ *
1577+ * On error, the contents of *data are undefined.
1578+ */
1579+int
1580+i915_gem_pread_ioctl(struct drm_device *dev, void *data,
1581+ struct drm_file *file_priv)
1582+{
1583+ struct drm_i915_gem_pread *args = data;
1584+ struct drm_gem_object *obj;
1585+ struct drm_i915_gem_object *obj_priv;
1586+ ssize_t read;
1587+ loff_t offset;
1588+ int ret;
1589+
1590+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1591+ if (obj == NULL)
1592+ return -EBADF;
1593+ obj_priv = obj->driver_private;
1594+
1595+ /* Bounds check source.
1596+ *
1597+ * XXX: This could use review for overflow issues...
1598+ */
1599+ if (args->offset > obj->size || args->size > obj->size ||
1600+ args->offset + args->size > obj->size) {
1601+ drm_gem_object_unreference(obj);
1602+ return -EINVAL;
1603+ }
1604+
1605+ mutex_lock(&dev->struct_mutex);
1606+
1607+ ret = i915_gem_object_set_domain_range(obj, args->offset, args->size,
1608+ I915_GEM_DOMAIN_CPU, 0);
1609+ if (ret != 0) {
1610+ drm_gem_object_unreference(obj);
1611+ mutex_unlock(&dev->struct_mutex);
1612+ }
1613+
1614+ offset = args->offset;
1615+
1616+ read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
1617+ args->size, &offset);
1618+ if (read != args->size) {
1619+ drm_gem_object_unreference(obj);
1620+ mutex_unlock(&dev->struct_mutex);
1621+ if (read < 0)
1622+ return read;
1623+ else
1624+ return -EINVAL;
1625+ }
1626+
1627+ drm_gem_object_unreference(obj);
1628+ mutex_unlock(&dev->struct_mutex);
1629+
1630+ return 0;
1631+}
1632+
1633+static int
1634+i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
1635+ struct drm_i915_gem_pwrite *args,
1636+ struct drm_file *file_priv)
1637+{
1638+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
1639+ ssize_t remain;
1640+ loff_t offset;
1641+ char __user *user_data;
1642+ char *vaddr;
1643+ int i, o, l;
1644+ int ret = 0;
1645+ unsigned long pfn;
1646+ unsigned long unwritten;
1647+
1648+ user_data = (char __user *) (uintptr_t) args->data_ptr;
1649+ remain = args->size;
1650+ if (!access_ok(VERIFY_READ, user_data, remain))
1651+ return -EFAULT;
1652+
1653+
1654+ mutex_lock(&dev->struct_mutex);
1655+ ret = i915_gem_object_pin(obj, 0);
1656+ if (ret) {
1657+ mutex_unlock(&dev->struct_mutex);
1658+ return ret;
1659+ }
1660+ ret = i915_gem_set_domain(obj, file_priv,
1661+ I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
1662+ if (ret)
1663+ goto fail;
1664+
1665+ obj_priv = obj->driver_private;
1666+ offset = obj_priv->gtt_offset + args->offset;
1667+ obj_priv->dirty = 1;
1668+
1669+ while (remain > 0) {
1670+ /* Operation in this page
1671+ *
1672+ * i = page number
1673+ * o = offset within page
1674+ * l = bytes to copy
1675+ */
1676+ i = offset >> PAGE_SHIFT;
1677+ o = offset & (PAGE_SIZE-1);
1678+ l = remain;
1679+ if ((o + l) > PAGE_SIZE)
1680+ l = PAGE_SIZE - o;
1681+
1682+ pfn = (dev->agp->base >> PAGE_SHIFT) + i;
1683+
1684+#ifdef DRM_KMAP_ATOMIC_PROT_PFN
1685+ /* kmap_atomic can't map IO pages on non-HIGHMEM kernels
1686+ */
1687+ vaddr = kmap_atomic_prot_pfn(pfn, KM_USER0,
1688+ __pgprot(__PAGE_KERNEL));
1689+#if WATCH_PWRITE
1690+ DRM_INFO("pwrite i %d o %d l %d pfn %ld vaddr %p\n",
1691+ i, o, l, pfn, vaddr);
1692+#endif
1693+ unwritten = __copy_from_user_inatomic_nocache(vaddr + o,
1694+ user_data, l);
1695+ kunmap_atomic(vaddr, KM_USER0);
1696+
1697+ if (unwritten)
1698+#endif
1699+ {
1700+ vaddr = ioremap(pfn << PAGE_SHIFT, PAGE_SIZE);
1701+#if WATCH_PWRITE
1702+ DRM_INFO("pwrite slow i %d o %d l %d "
1703+ "pfn %ld vaddr %p\n",
1704+ i, o, l, pfn, vaddr);
1705+#endif
1706+ if (vaddr == NULL) {
1707+ ret = -EFAULT;
1708+ goto fail;
1709+ }
1710+ unwritten = __copy_from_user(vaddr + o, user_data, l);
1711+#if WATCH_PWRITE
1712+ DRM_INFO("unwritten %ld\n", unwritten);
1713+#endif
1714+ iounmap(vaddr);
1715+ if (unwritten) {
1716+ ret = -EFAULT;
1717+ goto fail;
1718+ }
1719+ }
1720+
1721+ remain -= l;
1722+ user_data += l;
1723+ offset += l;
1724+ }
1725+#if WATCH_PWRITE && 1
1726+ i915_gem_clflush_object(obj);
1727+ i915_gem_dump_object(obj, args->offset + args->size, __func__, ~0);
1728+ i915_gem_clflush_object(obj);
1729+#endif
1730+
1731+fail:
1732+ i915_gem_object_unpin(obj);
1733+ mutex_unlock(&dev->struct_mutex);
1734+
1735+ return ret;
1736+}
1737+
1738+int
1739+i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
1740+ struct drm_i915_gem_pwrite *args,
1741+ struct drm_file *file_priv)
1742+{
1743+ int ret;
1744+ loff_t offset;
1745+ ssize_t written;
1746+
1747+ mutex_lock(&dev->struct_mutex);
1748+
1749+ ret = i915_gem_set_domain(obj, file_priv,
1750+ I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
1751+ if (ret) {
1752+ mutex_unlock(&dev->struct_mutex);
1753+ return ret;
1754+ }
1755+
1756+ offset = args->offset;
1757+
1758+ written = vfs_write(obj->filp,
1759+ (char __user *)(uintptr_t) args->data_ptr,
1760+ args->size, &offset);
1761+ if (written != args->size) {
1762+ mutex_unlock(&dev->struct_mutex);
1763+ if (written < 0)
1764+ return written;
1765+ else
1766+ return -EINVAL;
1767+ }
1768+
1769+ mutex_unlock(&dev->struct_mutex);
1770+
1771+ return 0;
1772+}
1773+
1774+/**
1775+ * Writes data to the object referenced by handle.
1776+ *
1777+ * On error, the contents of the buffer that were to be modified are undefined.
1778+ */
1779+int
1780+i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1781+ struct drm_file *file_priv)
1782+{
1783+ struct drm_i915_gem_pwrite *args = data;
1784+ struct drm_gem_object *obj;
1785+ struct drm_i915_gem_object *obj_priv;
1786+ int ret = 0;
1787+
1788+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1789+ if (obj == NULL)
1790+ return -EBADF;
1791+ obj_priv = obj->driver_private;
1792+
1793+ /* Bounds check destination.
1794+ *
1795+ * XXX: This could use review for overflow issues...
1796+ */
1797+ if (args->offset > obj->size || args->size > obj->size ||
1798+ args->offset + args->size > obj->size) {
1799+ drm_gem_object_unreference(obj);
1800+ return -EINVAL;
1801+ }
1802+
1803+ /* We can only do the GTT pwrite on untiled buffers, as otherwise
1804+ * it would end up going through the fenced access, and we'll get
1805+ * different detiling behavior between reading and writing.
1806+ * pread/pwrite currently are reading and writing from the CPU
1807+ * perspective, requiring manual detiling by the client.
1808+ */
1809+ if (obj_priv->tiling_mode == I915_TILING_NONE &&
1810+ dev->gtt_total != 0)
1811+ ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
1812+ else
1813+ ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
1814+
1815+#if WATCH_PWRITE
1816+ if (ret)
1817+ DRM_INFO("pwrite failed %d\n", ret);
1818+#endif
1819+
1820+ drm_gem_object_unreference(obj);
1821+
1822+ return ret;
1823+}
1824+
1825+/**
1826+ * Called when user space prepares to use an object
1827+ */
1828+int
1829+i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1830+ struct drm_file *file_priv)
1831+{
1832+ struct drm_i915_gem_set_domain *args = data;
1833+ struct drm_gem_object *obj;
1834+ int ret;
1835+
1836+ if (!(dev->driver->driver_features & DRIVER_GEM))
1837+ return -ENODEV;
1838+
1839+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1840+ if (obj == NULL)
1841+ return -EBADF;
1842+
1843+ mutex_lock(&dev->struct_mutex);
1844+#if WATCH_BUF
1845+ DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
1846+ obj, obj->size, args->read_domains, args->write_domain);
1847+#endif
1848+ ret = i915_gem_set_domain(obj, file_priv,
1849+ args->read_domains, args->write_domain);
1850+ drm_gem_object_unreference(obj);
1851+ mutex_unlock(&dev->struct_mutex);
1852+ return ret;
1853+}
1854+
1855+/**
1856+ * Called when user space has done writes to this buffer
1857+ */
1858+int
1859+i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1860+ struct drm_file *file_priv)
1861+{
1862+ struct drm_i915_gem_sw_finish *args = data;
1863+ struct drm_gem_object *obj;
1864+ struct drm_i915_gem_object *obj_priv;
1865+ int ret = 0;
1866+
1867+ if (!(dev->driver->driver_features & DRIVER_GEM))
1868+ return -ENODEV;
1869+
1870+ mutex_lock(&dev->struct_mutex);
1871+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1872+ if (obj == NULL) {
1873+ mutex_unlock(&dev->struct_mutex);
1874+ return -EBADF;
1875+ }
1876+
1877+#if WATCH_BUF
1878+ DRM_INFO("%s: sw_finish %d (%p %d)\n",
1879+ __func__, args->handle, obj, obj->size);
1880+#endif
1881+ obj_priv = obj->driver_private;
1882+
1883+ /* Pinned buffers may be scanout, so flush the cache */
1884+ if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) {
1885+ i915_gem_clflush_object(obj);
1886+ drm_agp_chipset_flush(dev);
1887+ }
1888+ drm_gem_object_unreference(obj);
1889+ mutex_unlock(&dev->struct_mutex);
1890+ return ret;
1891+}
1892+
1893+/**
1894+ * Maps the contents of an object, returning the address it is mapped
1895+ * into.
1896+ *
1897+ * While the mapping holds a reference on the contents of the object, it doesn't
1898+ * imply a ref on the object itself.
1899+ */
1900+int
1901+i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1902+ struct drm_file *file_priv)
1903+{
1904+ struct drm_i915_gem_mmap *args = data;
1905+ struct drm_gem_object *obj;
1906+ loff_t offset;
1907+ unsigned long addr;
1908+
1909+ if (!(dev->driver->driver_features & DRIVER_GEM))
1910+ return -ENODEV;
1911+
1912+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1913+ if (obj == NULL)
1914+ return -EBADF;
1915+
1916+ offset = args->offset;
1917+
1918+ down_write(&current->mm->mmap_sem);
1919+ addr = do_mmap(obj->filp, 0, args->size,
1920+ PROT_READ | PROT_WRITE, MAP_SHARED,
1921+ args->offset);
1922+ up_write(&current->mm->mmap_sem);
1923+ mutex_lock(&dev->struct_mutex);
1924+ drm_gem_object_unreference(obj);
1925+ mutex_unlock(&dev->struct_mutex);
1926+ if (IS_ERR((void *)addr))
1927+ return addr;
1928+
1929+ args->addr_ptr = (uint64_t) addr;
1930+
1931+ return 0;
1932+}
1933+
1934+static void
1935+i915_gem_object_free_page_list(struct drm_gem_object *obj)
1936+{
1937+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
1938+ int page_count = obj->size / PAGE_SIZE;
1939+ int i;
1940+
1941+ if (obj_priv->page_list == NULL)
1942+ return;
1943+
1944+
1945+ for (i = 0; i < page_count; i++)
1946+ if (obj_priv->page_list[i] != NULL) {
1947+ if (obj_priv->dirty)
1948+ set_page_dirty(obj_priv->page_list[i]);
1949+ mark_page_accessed(obj_priv->page_list[i]);
1950+ page_cache_release(obj_priv->page_list[i]);
1951+ }
1952+ obj_priv->dirty = 0;
1953+
1954+ drm_free(obj_priv->page_list,
1955+ page_count * sizeof(struct page *),
1956+ DRM_MEM_DRIVER);
1957+ obj_priv->page_list = NULL;
1958+}
1959+
1960+static void
1961+i915_gem_object_move_to_active(struct drm_gem_object *obj)
1962+{
1963+ struct drm_device *dev = obj->dev;
1964+ drm_i915_private_t *dev_priv = dev->dev_private;
1965+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
1966+
1967+ /* Add a reference if we're newly entering the active list. */
1968+ if (!obj_priv->active) {
1969+ drm_gem_object_reference(obj);
1970+ obj_priv->active = 1;
1971+ }
1972+ /* Move from whatever list we were on to the tail of execution. */
1973+ list_move_tail(&obj_priv->list,
1974+ &dev_priv->mm.active_list);
1975+}
1976+
1977+
1978+static void
1979+i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1980+{
1981+ struct drm_device *dev = obj->dev;
1982+ drm_i915_private_t *dev_priv = dev->dev_private;
1983+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
1984+
1985+ i915_verify_inactive(dev, __FILE__, __LINE__);
1986+ if (obj_priv->pin_count != 0)
1987+ list_del_init(&obj_priv->list);
1988+ else
1989+ list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1990+
1991+ if (obj_priv->active) {
1992+ obj_priv->active = 0;
1993+ drm_gem_object_unreference(obj);
1994+ }
1995+ i915_verify_inactive(dev, __FILE__, __LINE__);
1996+}
1997+
1998+/**
1999+ * Creates a new sequence number, emitting a write of it to the status page
2000+ * plus an interrupt, which will trigger i915_user_interrupt_handler.
2001+ *
2002+ * Must be called with struct_lock held.
2003+ *
2004+ * Returned sequence numbers are nonzero on success.
2005+ */
2006+static uint32_t
2007+i915_add_request(struct drm_device *dev, uint32_t flush_domains)
2008+{
2009+ drm_i915_private_t *dev_priv = dev->dev_private;
2010+ struct drm_i915_gem_request *request;
2011+ uint32_t seqno;
2012+ int was_empty;
2013+ RING_LOCALS;
2014+
2015+ request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
2016+ if (request == NULL)
2017+ return 0;
2018+
2019+ /* Grab the seqno we're going to make this request be, and bump the
2020+ * next (skipping 0 so it can be the reserved no-seqno value).
2021+ */
2022+ seqno = dev_priv->mm.next_gem_seqno;
2023+ dev_priv->mm.next_gem_seqno++;
2024+ if (dev_priv->mm.next_gem_seqno == 0)
2025+ dev_priv->mm.next_gem_seqno++;
2026+
2027+ BEGIN_LP_RING(4);
2028+ OUT_RING(MI_STORE_DWORD_INDEX);
2029+ OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
2030+ OUT_RING(seqno);
2031+
2032+ OUT_RING(MI_USER_INTERRUPT);
2033+ ADVANCE_LP_RING();
2034+
2035+ DRM_DEBUG("%d\n", seqno);
2036+
2037+ request->seqno = seqno;
2038+ request->emitted_jiffies = jiffies;
2039+ request->flush_domains = flush_domains;
2040+ was_empty = list_empty(&dev_priv->mm.request_list);
2041+ list_add_tail(&request->list, &dev_priv->mm.request_list);
2042+
2043+ if (was_empty)
2044+ schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
2045+ return seqno;
2046+}
2047+
2048+/**
2049+ * Command execution barrier
2050+ *
2051+ * Ensures that all commands in the ring are finished
2052+ * before signalling the CPU
2053+ */
2054+uint32_t
2055+i915_retire_commands(struct drm_device *dev)
2056+{
2057+ drm_i915_private_t *dev_priv = dev->dev_private;
2058+ uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
2059+ uint32_t flush_domains = 0;
2060+ RING_LOCALS;
2061+
2062+ /* The sampler always gets flushed on i965 (sigh) */
2063+ if (IS_I965G(dev))
2064+ flush_domains |= I915_GEM_DOMAIN_SAMPLER;
2065+ BEGIN_LP_RING(2);
2066+ OUT_RING(cmd);
2067+ OUT_RING(0); /* noop */
2068+ ADVANCE_LP_RING();
2069+ return flush_domains;
2070+}
2071+
2072+/**
2073+ * Moves buffers associated only with the given active seqno from the active
2074+ * to inactive list, potentially freeing them.
2075+ */
2076+static void
2077+i915_gem_retire_request(struct drm_device *dev,
2078+ struct drm_i915_gem_request *request)
2079+{
2080+ drm_i915_private_t *dev_priv = dev->dev_private;
2081+
2082+ /* Move any buffers on the active list that are no longer referenced
2083+ * by the ringbuffer to the flushing/inactive lists as appropriate.
2084+ */
2085+ while (!list_empty(&dev_priv->mm.active_list)) {
2086+ struct drm_gem_object *obj;
2087+ struct drm_i915_gem_object *obj_priv;
2088+
2089+ obj_priv = list_first_entry(&dev_priv->mm.active_list,
2090+ struct drm_i915_gem_object,
2091+ list);
2092+ obj = obj_priv->obj;
2093+
2094+ /* If the seqno being retired doesn't match the oldest in the
2095+ * list, then the oldest in the list must still be newer than
2096+ * this seqno.
2097+ */
2098+ if (obj_priv->last_rendering_seqno != request->seqno)
2099+ return;
2100+#if WATCH_LRU
2101+ DRM_INFO("%s: retire %d moves to inactive list %p\n",
2102+ __func__, request->seqno, obj);
2103+#endif
2104+
2105+ if (obj->write_domain != 0) {
2106+ list_move_tail(&obj_priv->list,
2107+ &dev_priv->mm.flushing_list);
2108+ } else {
2109+ i915_gem_object_move_to_inactive(obj);
2110+ }
2111+ }
2112+
2113+ if (request->flush_domains != 0) {
2114+ struct drm_i915_gem_object *obj_priv, *next;
2115+
2116+ /* Clear the write domain and activity from any buffers
2117+ * that are just waiting for a flush matching the one retired.
2118+ */
2119+ list_for_each_entry_safe(obj_priv, next,
2120+ &dev_priv->mm.flushing_list, list) {
2121+ struct drm_gem_object *obj = obj_priv->obj;
2122+
2123+ if (obj->write_domain & request->flush_domains) {
2124+ obj->write_domain = 0;
2125+ i915_gem_object_move_to_inactive(obj);
2126+ }
2127+ }
2128+
2129+ }
2130+}
2131+
2132+/**
2133+ * Returns true if seq1 is later than seq2.
2134+ */
2135+static int
2136+i915_seqno_passed(uint32_t seq1, uint32_t seq2)
2137+{
2138+ return (int32_t)(seq1 - seq2) >= 0;
2139+}
2140+
2141+uint32_t
2142+i915_get_gem_seqno(struct drm_device *dev)
2143+{
2144+ drm_i915_private_t *dev_priv = dev->dev_private;
2145+
2146+ return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
2147+}
2148+
2149+/**
2150+ * This function clears the request list as sequence numbers are passed.
2151+ */
2152+void
2153+i915_gem_retire_requests(struct drm_device *dev)
2154+{
2155+ drm_i915_private_t *dev_priv = dev->dev_private;
2156+ uint32_t seqno;
2157+
2158+ seqno = i915_get_gem_seqno(dev);
2159+
2160+ while (!list_empty(&dev_priv->mm.request_list)) {
2161+ struct drm_i915_gem_request *request;
2162+ uint32_t retiring_seqno;
2163+
2164+ request = list_first_entry(&dev_priv->mm.request_list,
2165+ struct drm_i915_gem_request,
2166+ list);
2167+ retiring_seqno = request->seqno;
2168+
2169+ if (i915_seqno_passed(seqno, retiring_seqno) ||
2170+ dev_priv->mm.wedged) {
2171+ i915_gem_retire_request(dev, request);
2172+
2173+ list_del(&request->list);
2174+ drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
2175+ } else
2176+ break;
2177+ }
2178+}
2179+
2180+void
2181+i915_gem_retire_work_handler(struct work_struct *work)
2182+{
2183+ drm_i915_private_t *dev_priv;
2184+ struct drm_device *dev;
2185+
2186+ dev_priv = container_of(work, drm_i915_private_t,
2187+ mm.retire_work.work);
2188+ dev = dev_priv->dev;
2189+
2190+ mutex_lock(&dev->struct_mutex);
2191+ i915_gem_retire_requests(dev);
2192+ if (!list_empty(&dev_priv->mm.request_list))
2193+ schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
2194+ mutex_unlock(&dev->struct_mutex);
2195+}
2196+
2197+/**
2198+ * Waits for a sequence number to be signaled, and cleans up the
2199+ * request and object lists appropriately for that event.
2200+ */
2201+int
2202+i915_wait_request(struct drm_device *dev, uint32_t seqno)
2203+{
2204+ drm_i915_private_t *dev_priv = dev->dev_private;
2205+ int ret = 0;
2206+
2207+ BUG_ON(seqno == 0);
2208+
2209+ if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
2210+ dev_priv->mm.waiting_gem_seqno = seqno;
2211+ i915_user_irq_get(dev);
2212+ ret = wait_event_interruptible(dev_priv->irq_queue,
2213+ i915_seqno_passed(i915_get_gem_seqno(dev),
2214+ seqno) ||
2215+ dev_priv->mm.wedged);
2216+ i915_user_irq_put(dev);
2217+ dev_priv->mm.waiting_gem_seqno = 0;
2218+ }
2219+ if (dev_priv->mm.wedged)
2220+ ret = -EIO;
2221+
2222+ if (ret && ret != -ERESTARTSYS)
2223+ DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
2224+ __func__, ret, seqno, i915_get_gem_seqno(dev));
2225+
2226+ /* Directly dispatch request retiring. While we have the work queue
2227+ * to handle this, the waiter on a request often wants an associated
2228+ * buffer to have made it to the inactive list, and we would need
2229+ * a separate wait queue to handle that.
2230+ */
2231+ if (ret == 0)
2232+ i915_gem_retire_requests(dev);
2233+
2234+ return ret;
2235+}
2236+
2237+static void
2238+i915_gem_flush(struct drm_device *dev,
2239+ uint32_t invalidate_domains,
2240+ uint32_t flush_domains)
2241+{
2242+ drm_i915_private_t *dev_priv = dev->dev_private;
2243+ uint32_t cmd;
2244+ RING_LOCALS;
2245+
2246+#if WATCH_EXEC
2247+ DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
2248+ invalidate_domains, flush_domains);
2249+#endif
2250+
2251+ if (flush_domains & I915_GEM_DOMAIN_CPU)
2252+ drm_agp_chipset_flush(dev);
2253+
2254+ if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
2255+ I915_GEM_DOMAIN_GTT)) {
2256+ /*
2257+ * read/write caches:
2258+ *
2259+ * I915_GEM_DOMAIN_RENDER is always invalidated, but is
2260+ * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
2261+ * also flushed at 2d versus 3d pipeline switches.
2262+ *
2263+ * read-only caches:
2264+ *
2265+ * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
2266+ * MI_READ_FLUSH is set, and is always flushed on 965.
2267+ *
2268+ * I915_GEM_DOMAIN_COMMAND may not exist?
2269+ *
2270+ * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
2271+ * invalidated when MI_EXE_FLUSH is set.
2272+ *
2273+ * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
2274+ * invalidated with every MI_FLUSH.
2275+ *
2276+ * TLBs:
2277+ *
2278+ * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
2279+ * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
2280+ * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
2281+ * are flushed at any MI_FLUSH.
2282+ */
2283+
2284+ cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
2285+ if ((invalidate_domains|flush_domains) &
2286+ I915_GEM_DOMAIN_RENDER)
2287+ cmd &= ~MI_NO_WRITE_FLUSH;
2288+ if (!IS_I965G(dev)) {
2289+ /*
2290+ * On the 965, the sampler cache always gets flushed
2291+ * and this bit is reserved.
2292+ */
2293+ if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
2294+ cmd |= MI_READ_FLUSH;
2295+ }
2296+ if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
2297+ cmd |= MI_EXE_FLUSH;
2298+
2299+#if WATCH_EXEC
2300+ DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
2301+#endif
2302+ BEGIN_LP_RING(2);
2303+ OUT_RING(cmd);
2304+ OUT_RING(0); /* noop */
2305+ ADVANCE_LP_RING();
2306+ }
2307+}
2308+
2309+/**
2310+ * Ensures that all rendering to the object has completed and the object is
2311+ * safe to unbind from the GTT or access from the CPU.
2312+ */
2313+static int
2314+i915_gem_object_wait_rendering(struct drm_gem_object *obj)
2315+{
2316+ struct drm_device *dev = obj->dev;
2317+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
2318+ int ret;
2319+
2320+ /* If there are writes queued to the buffer, flush and
2321+ * create a new seqno to wait for.
2322+ */
2323+ if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) {
2324+ uint32_t write_domain = obj->write_domain;
2325+#if WATCH_BUF
2326+ DRM_INFO("%s: flushing object %p from write domain %08x\n",
2327+ __func__, obj, write_domain);
2328+#endif
2329+ i915_gem_flush(dev, 0, write_domain);
2330+
2331+ i915_gem_object_move_to_active(obj);
2332+ obj_priv->last_rendering_seqno = i915_add_request(dev,
2333+ write_domain);
2334+ BUG_ON(obj_priv->last_rendering_seqno == 0);
2335+#if WATCH_LRU
2336+ DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj);
2337+#endif
2338+ }
2339+
2340+ /* If there is rendering queued on the buffer being evicted, wait for
2341+ * it.
2342+ */
2343+ if (obj_priv->active) {
2344+#if WATCH_BUF
2345+ DRM_INFO("%s: object %p wait for seqno %08x\n",
2346+ __func__, obj, obj_priv->last_rendering_seqno);
2347+#endif
2348+ ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
2349+ if (ret != 0)
2350+ return ret;
2351+ }
2352+
2353+ return 0;
2354+}
2355+
2356+/**
2357+ * Unbinds an object from the GTT aperture.
2358+ */
2359+static int
2360+i915_gem_object_unbind(struct drm_gem_object *obj)
2361+{
2362+ struct drm_device *dev = obj->dev;
2363+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
2364+ int ret = 0;
2365+
2366+#if WATCH_BUF
2367+ DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
2368+ DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
2369+#endif
2370+ if (obj_priv->gtt_space == NULL)
2371+ return 0;
2372+
2373+ if (obj_priv->pin_count != 0) {
2374+ DRM_ERROR("Attempting to unbind pinned buffer\n");
2375+ return -EINVAL;
2376+ }
2377+
2378+ /* Wait for any rendering to complete
2379+ */
2380+ ret = i915_gem_object_wait_rendering(obj);
2381+ if (ret) {
2382+ DRM_ERROR("wait_rendering failed: %d\n", ret);
2383+ return ret;
2384+ }
2385+
2386+ /* Move the object to the CPU domain to ensure that
2387+ * any possible CPU writes while it's not in the GTT
2388+ * are flushed when we go to remap it. This will
2389+ * also ensure that all pending GPU writes are finished
2390+ * before we unbind.
2391+ */
2392+ ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU,
2393+ I915_GEM_DOMAIN_CPU);
2394+ if (ret) {
2395+ DRM_ERROR("set_domain failed: %d\n", ret);
2396+ return ret;
2397+ }
2398+
2399+ if (obj_priv->agp_mem != NULL) {
2400+ drm_unbind_agp(obj_priv->agp_mem);
2401+ drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2402+ obj_priv->agp_mem = NULL;
2403+ }
2404+
2405+ BUG_ON(obj_priv->active);
2406+
2407+ i915_gem_object_free_page_list(obj);
2408+
2409+ if (obj_priv->gtt_space) {
2410+ atomic_dec(&dev->gtt_count);
2411+ atomic_sub(obj->size, &dev->gtt_memory);
2412+
2413+ drm_mm_put_block(obj_priv->gtt_space);
2414+ obj_priv->gtt_space = NULL;
2415+ }
2416+
2417+ /* Remove ourselves from the LRU list if present. */
2418+ if (!list_empty(&obj_priv->list))
2419+ list_del_init(&obj_priv->list);
2420+
2421+ return 0;
2422+}
2423+
2424+static int
2425+i915_gem_evict_something(struct drm_device *dev)
2426+{
2427+ drm_i915_private_t *dev_priv = dev->dev_private;
2428+ struct drm_gem_object *obj;
2429+ struct drm_i915_gem_object *obj_priv;
2430+ int ret = 0;
2431+
2432+ for (;;) {
2433+ /* If there's an inactive buffer available now, grab it
2434+ * and be done.
2435+ */
2436+ if (!list_empty(&dev_priv->mm.inactive_list)) {
2437+ obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
2438+ struct drm_i915_gem_object,
2439+ list);
2440+ obj = obj_priv->obj;
2441+ BUG_ON(obj_priv->pin_count != 0);
2442+#if WATCH_LRU
2443+ DRM_INFO("%s: evicting %p\n", __func__, obj);
2444+#endif
2445+ BUG_ON(obj_priv->active);
2446+
2447+ /* Wait on the rendering and unbind the buffer. */
2448+ ret = i915_gem_object_unbind(obj);
2449+ break;
2450+ }
2451+
2452+ /* If we didn't get anything, but the ring is still processing
2453+ * things, wait for one of those things to finish and hopefully
2454+ * leave us a buffer to evict.
2455+ */
2456+ if (!list_empty(&dev_priv->mm.request_list)) {
2457+ struct drm_i915_gem_request *request;
2458+
2459+ request = list_first_entry(&dev_priv->mm.request_list,
2460+ struct drm_i915_gem_request,
2461+ list);
2462+
2463+ ret = i915_wait_request(dev, request->seqno);
2464+ if (ret)
2465+ break;
2466+
2467+ /* if waiting caused an object to become inactive,
2468+ * then loop around and wait for it. Otherwise, we
2469+ * assume that waiting freed and unbound something,
2470+ * so there should now be some space in the GTT
2471+ */
2472+ if (!list_empty(&dev_priv->mm.inactive_list))
2473+ continue;
2474+ break;
2475+ }
2476+
2477+ /* If we didn't have anything on the request list but there
2478+ * are buffers awaiting a flush, emit one and try again.
2479+ * When we wait on it, those buffers waiting for that flush
2480+ * will get moved to inactive.
2481+ */
2482+ if (!list_empty(&dev_priv->mm.flushing_list)) {
2483+ obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2484+ struct drm_i915_gem_object,
2485+ list);
2486+ obj = obj_priv->obj;
2487+
2488+ i915_gem_flush(dev,
2489+ obj->write_domain,
2490+ obj->write_domain);
2491+ i915_add_request(dev, obj->write_domain);
2492+
2493+ obj = NULL;
2494+ continue;
2495+ }
2496+
2497+ DRM_ERROR("inactive empty %d request empty %d "
2498+ "flushing empty %d\n",
2499+ list_empty(&dev_priv->mm.inactive_list),
2500+ list_empty(&dev_priv->mm.request_list),
2501+ list_empty(&dev_priv->mm.flushing_list));
2502+ /* If we didn't do any of the above, there's nothing to be done
2503+ * and we just can't fit it in.
2504+ */
2505+ return -ENOMEM;
2506+ }
2507+ return ret;
2508+}
2509+
2510+static int
2511+i915_gem_object_get_page_list(struct drm_gem_object *obj)
2512+{
2513+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
2514+ int page_count, i;
2515+ struct address_space *mapping;
2516+ struct inode *inode;
2517+ struct page *page;
2518+ int ret;
2519+
2520+ if (obj_priv->page_list)
2521+ return 0;
2522+
2523+ /* Get the list of pages out of our struct file. They'll be pinned
2524+ * at this point until we release them.
2525+ */
2526+ page_count = obj->size / PAGE_SIZE;
2527+ BUG_ON(obj_priv->page_list != NULL);
2528+ obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
2529+ DRM_MEM_DRIVER);
2530+ if (obj_priv->page_list == NULL) {
2531+ DRM_ERROR("Faled to allocate page list\n");
2532+ return -ENOMEM;
2533+ }
2534+
2535+ inode = obj->filp->f_path.dentry->d_inode;
2536+ mapping = inode->i_mapping;
2537+ for (i = 0; i < page_count; i++) {
2538+ page = read_mapping_page(mapping, i, NULL);
2539+ if (IS_ERR(page)) {
2540+ ret = PTR_ERR(page);
2541+ DRM_ERROR("read_mapping_page failed: %d\n", ret);
2542+ i915_gem_object_free_page_list(obj);
2543+ return ret;
2544+ }
2545+ obj_priv->page_list[i] = page;
2546+ }
2547+ return 0;
2548+}
2549+
2550+/**
2551+ * Finds free space in the GTT aperture and binds the object there.
2552+ */
2553+static int
2554+i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2555+{
2556+ struct drm_device *dev = obj->dev;
2557+ drm_i915_private_t *dev_priv = dev->dev_private;
2558+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
2559+ struct drm_mm_node *free_space;
2560+ int page_count, ret;
2561+
2562+ if (alignment == 0)
2563+ alignment = PAGE_SIZE;
2564+ if (alignment & (PAGE_SIZE - 1)) {
2565+ DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2566+ return -EINVAL;
2567+ }
2568+
2569+ search_free:
2570+ free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2571+ obj->size, alignment, 0);
2572+ if (free_space != NULL) {
2573+ obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2574+ alignment);
2575+ if (obj_priv->gtt_space != NULL) {
2576+ obj_priv->gtt_space->private = obj;
2577+ obj_priv->gtt_offset = obj_priv->gtt_space->start;
2578+ }
2579+ }
2580+ if (obj_priv->gtt_space == NULL) {
2581+ /* If the gtt is empty and we're still having trouble
2582+ * fitting our object in, we're out of memory.
2583+ */
2584+#if WATCH_LRU
2585+ DRM_INFO("%s: GTT full, evicting something\n", __func__);
2586+#endif
2587+ if (list_empty(&dev_priv->mm.inactive_list) &&
2588+ list_empty(&dev_priv->mm.flushing_list) &&
2589+ list_empty(&dev_priv->mm.active_list)) {
2590+ DRM_ERROR("GTT full, but LRU list empty\n");
2591+ return -ENOMEM;
2592+ }
2593+
2594+ ret = i915_gem_evict_something(dev);
2595+ if (ret != 0) {
2596+ DRM_ERROR("Failed to evict a buffer %d\n", ret);
2597+ return ret;
2598+ }
2599+ goto search_free;
2600+ }
2601+
2602+#if WATCH_BUF
2603+ DRM_INFO("Binding object of size %d at 0x%08x\n",
2604+ obj->size, obj_priv->gtt_offset);
2605+#endif
2606+ ret = i915_gem_object_get_page_list(obj);
2607+ if (ret) {
2608+ drm_mm_put_block(obj_priv->gtt_space);
2609+ obj_priv->gtt_space = NULL;
2610+ return ret;
2611+ }
2612+
2613+ page_count = obj->size / PAGE_SIZE;
2614+ /* Create an AGP memory structure pointing at our pages, and bind it
2615+ * into the GTT.
2616+ */
2617+ obj_priv->agp_mem = drm_agp_bind_pages(dev,
2618+ obj_priv->page_list,
2619+ page_count,
2620+ obj_priv->gtt_offset);
2621+ if (obj_priv->agp_mem == NULL) {
2622+ i915_gem_object_free_page_list(obj);
2623+ drm_mm_put_block(obj_priv->gtt_space);
2624+ obj_priv->gtt_space = NULL;
2625+ return -ENOMEM;
2626+ }
2627+ atomic_inc(&dev->gtt_count);
2628+ atomic_add(obj->size, &dev->gtt_memory);
2629+
2630+ /* Assert that the object is not currently in any GPU domain. As it
2631+ * wasn't in the GTT, there shouldn't be any way it could have been in
2632+ * a GPU cache
2633+ */
2634+ BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2635+ BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2636+
2637+ return 0;
2638+}
2639+
2640+void
2641+i915_gem_clflush_object(struct drm_gem_object *obj)
2642+{
2643+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
2644+
2645+ /* If we don't have a page list set up, then we're not pinned
2646+ * to GPU, and we can ignore the cache flush because it'll happen
2647+ * again at bind time.
2648+ */
2649+ if (obj_priv->page_list == NULL)
2650+ return;
2651+
2652+ drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
2653+}
2654+
2655+/*
2656+ * Set the next domain for the specified object. This
2657+ * may not actually perform the necessary flushing/invaliding though,
2658+ * as that may want to be batched with other set_domain operations
2659+ *
2660+ * This is (we hope) the only really tricky part of gem. The goal
2661+ * is fairly simple -- track which caches hold bits of the object
2662+ * and make sure they remain coherent. A few concrete examples may
2663+ * help to explain how it works. For shorthand, we use the notation
2664+ * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2665+ * a pair of read and write domain masks.
2666+ *
2667+ * Case 1: the batch buffer
2668+ *
2669+ * 1. Allocated
2670+ * 2. Written by CPU
2671+ * 3. Mapped to GTT
2672+ * 4. Read by GPU
2673+ * 5. Unmapped from GTT
2674+ * 6. Freed
2675+ *
2676+ * Let's take these a step at a time
2677+ *
2678+ * 1. Allocated
2679+ * Pages allocated from the kernel may still have
2680+ * cache contents, so we set them to (CPU, CPU) always.
2681+ * 2. Written by CPU (using pwrite)
2682+ * The pwrite function calls set_domain (CPU, CPU) and
2683+ * this function does nothing (as nothing changes)
2684+ * 3. Mapped by GTT
2685+ * This function asserts that the object is not
2686+ * currently in any GPU-based read or write domains
2687+ * 4. Read by GPU
2688+ * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2689+ * As write_domain is zero, this function adds in the
2690+ * current read domains (CPU+COMMAND, 0).
2691+ * flush_domains is set to CPU.
2692+ * invalidate_domains is set to COMMAND
2693+ * clflush is run to get data out of the CPU caches
2694+ * then i915_dev_set_domain calls i915_gem_flush to
2695+ * emit an MI_FLUSH and drm_agp_chipset_flush
2696+ * 5. Unmapped from GTT
2697+ * i915_gem_object_unbind calls set_domain (CPU, CPU)
2698+ * flush_domains and invalidate_domains end up both zero
2699+ * so no flushing/invalidating happens
2700+ * 6. Freed
2701+ * yay, done
2702+ *
2703+ * Case 2: The shared render buffer
2704+ *
2705+ * 1. Allocated
2706+ * 2. Mapped to GTT
2707+ * 3. Read/written by GPU
2708+ * 4. set_domain to (CPU,CPU)
2709+ * 5. Read/written by CPU
2710+ * 6. Read/written by GPU
2711+ *
2712+ * 1. Allocated
2713+ * Same as last example, (CPU, CPU)
2714+ * 2. Mapped to GTT
2715+ * Nothing changes (assertions find that it is not in the GPU)
2716+ * 3. Read/written by GPU
2717+ * execbuffer calls set_domain (RENDER, RENDER)
2718+ * flush_domains gets CPU
2719+ * invalidate_domains gets GPU
2720+ * clflush (obj)
2721+ * MI_FLUSH and drm_agp_chipset_flush
2722+ * 4. set_domain (CPU, CPU)
2723+ * flush_domains gets GPU
2724+ * invalidate_domains gets CPU
2725+ * wait_rendering (obj) to make sure all drawing is complete.
2726+ * This will include an MI_FLUSH to get the data from GPU
2727+ * to memory
2728+ * clflush (obj) to invalidate the CPU cache
2729+ * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2730+ * 5. Read/written by CPU
2731+ * cache lines are loaded and dirtied
2732+ * 6. Read written by GPU
2733+ * Same as last GPU access
2734+ *
2735+ * Case 3: The constant buffer
2736+ *
2737+ * 1. Allocated
2738+ * 2. Written by CPU
2739+ * 3. Read by GPU
2740+ * 4. Updated (written) by CPU again
2741+ * 5. Read by GPU
2742+ *
2743+ * 1. Allocated
2744+ * (CPU, CPU)
2745+ * 2. Written by CPU
2746+ * (CPU, CPU)
2747+ * 3. Read by GPU
2748+ * (CPU+RENDER, 0)
2749+ * flush_domains = CPU
2750+ * invalidate_domains = RENDER
2751+ * clflush (obj)
2752+ * MI_FLUSH
2753+ * drm_agp_chipset_flush
2754+ * 4. Updated (written) by CPU again
2755+ * (CPU, CPU)
2756+ * flush_domains = 0 (no previous write domain)
2757+ * invalidate_domains = 0 (no new read domains)
2758+ * 5. Read by GPU
2759+ * (CPU+RENDER, 0)
2760+ * flush_domains = CPU
2761+ * invalidate_domains = RENDER
2762+ * clflush (obj)
2763+ * MI_FLUSH
2764+ * drm_agp_chipset_flush
2765+ */
2766+static int
2767+i915_gem_object_set_domain(struct drm_gem_object *obj,
2768+ uint32_t read_domains,
2769+ uint32_t write_domain)
2770+{
2771+ struct drm_device *dev = obj->dev;
2772+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
2773+ uint32_t invalidate_domains = 0;
2774+ uint32_t flush_domains = 0;
2775+ int ret;
2776+
2777+#if WATCH_BUF
2778+ DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2779+ __func__, obj,
2780+ obj->read_domains, read_domains,
2781+ obj->write_domain, write_domain);
2782+#endif
2783+ /*
2784+ * If the object isn't moving to a new write domain,
2785+ * let the object stay in multiple read domains
2786+ */
2787+ if (write_domain == 0)
2788+ read_domains |= obj->read_domains;
2789+ else
2790+ obj_priv->dirty = 1;
2791+
2792+ /*
2793+ * Flush the current write domain if
2794+ * the new read domains don't match. Invalidate
2795+ * any read domains which differ from the old
2796+ * write domain
2797+ */
2798+ if (obj->write_domain && obj->write_domain != read_domains) {
2799+ flush_domains |= obj->write_domain;
2800+ invalidate_domains |= read_domains & ~obj->write_domain;
2801+ }
2802+ /*
2803+ * Invalidate any read caches which may have
2804+ * stale data. That is, any new read domains.
2805+ */
2806+ invalidate_domains |= read_domains & ~obj->read_domains;
2807+ if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2808+#if WATCH_BUF
2809+ DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2810+ __func__, flush_domains, invalidate_domains);
2811+#endif
2812+ /*
2813+ * If we're invaliding the CPU cache and flushing a GPU cache,
2814+ * then pause for rendering so that the GPU caches will be
2815+ * flushed before the cpu cache is invalidated
2816+ */
2817+ if ((invalidate_domains & I915_GEM_DOMAIN_CPU) &&
2818+ (flush_domains & ~(I915_GEM_DOMAIN_CPU |
2819+ I915_GEM_DOMAIN_GTT))) {
2820+ ret = i915_gem_object_wait_rendering(obj);
2821+ if (ret)
2822+ return ret;
2823+ }
2824+ i915_gem_clflush_object(obj);
2825+ }
2826+
2827+ if ((write_domain | flush_domains) != 0)
2828+ obj->write_domain = write_domain;
2829+
2830+ /* If we're invalidating the CPU domain, clear the per-page CPU
2831+ * domain list as well.
2832+ */
2833+ if (obj_priv->page_cpu_valid != NULL &&
2834+ (obj->read_domains & I915_GEM_DOMAIN_CPU) &&
2835+ ((read_domains & I915_GEM_DOMAIN_CPU) == 0)) {
2836+ memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
2837+ }
2838+ obj->read_domains = read_domains;
2839+
2840+ dev->invalidate_domains |= invalidate_domains;
2841+ dev->flush_domains |= flush_domains;
2842+#if WATCH_BUF
2843+ DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2844+ __func__,
2845+ obj->read_domains, obj->write_domain,
2846+ dev->invalidate_domains, dev->flush_domains);
2847+#endif
2848+ return 0;
2849+}
2850+
2851+/**
2852+ * Set the read/write domain on a range of the object.
2853+ *
2854+ * Currently only implemented for CPU reads, otherwise drops to normal
2855+ * i915_gem_object_set_domain().
2856+ */
2857+static int
2858+i915_gem_object_set_domain_range(struct drm_gem_object *obj,
2859+ uint64_t offset,
2860+ uint64_t size,
2861+ uint32_t read_domains,
2862+ uint32_t write_domain)
2863+{
2864+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
2865+ int ret, i;
2866+
2867+ if (obj->read_domains & I915_GEM_DOMAIN_CPU)
2868+ return 0;
2869+
2870+ if (read_domains != I915_GEM_DOMAIN_CPU ||
2871+ write_domain != 0)
2872+ return i915_gem_object_set_domain(obj,
2873+ read_domains, write_domain);
2874+
2875+ /* Wait on any GPU rendering to the object to be flushed. */
2876+ if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) {
2877+ ret = i915_gem_object_wait_rendering(obj);
2878+ if (ret)
2879+ return ret;
2880+ }
2881+
2882+ if (obj_priv->page_cpu_valid == NULL) {
2883+ obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2884+ DRM_MEM_DRIVER);
2885+ }
2886+
2887+ /* Flush the cache on any pages that are still invalid from the CPU's
2888+ * perspective.
2889+ */
2890+ for (i = offset / PAGE_SIZE; i < (offset + size - 1) / PAGE_SIZE; i++) {
2891+ if (obj_priv->page_cpu_valid[i])
2892+ continue;
2893+
2894+ drm_clflush_pages(obj_priv->page_list + i, 1);
2895+
2896+ obj_priv->page_cpu_valid[i] = 1;
2897+ }
2898+
2899+ return 0;
2900+}
2901+
2902+/**
2903+ * Once all of the objects have been set in the proper domain,
2904+ * perform the necessary flush and invalidate operations.
2905+ *
2906+ * Returns the write domains flushed, for use in flush tracking.
2907+ */
2908+static uint32_t
2909+i915_gem_dev_set_domain(struct drm_device *dev)
2910+{
2911+ uint32_t flush_domains = dev->flush_domains;
2912+
2913+ /*
2914+ * Now that all the buffers are synced to the proper domains,
2915+ * flush and invalidate the collected domains
2916+ */
2917+ if (dev->invalidate_domains | dev->flush_domains) {
2918+#if WATCH_EXEC
2919+ DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
2920+ __func__,
2921+ dev->invalidate_domains,
2922+ dev->flush_domains);
2923+#endif
2924+ i915_gem_flush(dev,
2925+ dev->invalidate_domains,
2926+ dev->flush_domains);
2927+ dev->invalidate_domains = 0;
2928+ dev->flush_domains = 0;
2929+ }
2930+
2931+ return flush_domains;
2932+}
2933+
2934+/**
2935+ * Pin an object to the GTT and evaluate the relocations landing in it.
2936+ */
2937+static int
2938+i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2939+ struct drm_file *file_priv,
2940+ struct drm_i915_gem_exec_object *entry)
2941+{
2942+ struct drm_device *dev = obj->dev;
2943+ struct drm_i915_gem_relocation_entry reloc;
2944+ struct drm_i915_gem_relocation_entry __user *relocs;
2945+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
2946+ int i, ret;
2947+ uint32_t last_reloc_offset = -1;
2948+ void *reloc_page = NULL;
2949+
2950+ /* Choose the GTT offset for our buffer and put it there. */
2951+ ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2952+ if (ret)
2953+ return ret;
2954+
2955+ entry->offset = obj_priv->gtt_offset;
2956+
2957+ relocs = (struct drm_i915_gem_relocation_entry __user *)
2958+ (uintptr_t) entry->relocs_ptr;
2959+ /* Apply the relocations, using the GTT aperture to avoid cache
2960+ * flushing requirements.
2961+ */
2962+ for (i = 0; i < entry->relocation_count; i++) {
2963+ struct drm_gem_object *target_obj;
2964+ struct drm_i915_gem_object *target_obj_priv;
2965+ uint32_t reloc_val, reloc_offset, *reloc_entry;
2966+ int ret;
2967+
2968+ ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
2969+ if (ret != 0) {
2970+ i915_gem_object_unpin(obj);
2971+ return ret;
2972+ }
2973+
2974+ target_obj = drm_gem_object_lookup(obj->dev, file_priv,
2975+ reloc.target_handle);
2976+ if (target_obj == NULL) {
2977+ i915_gem_object_unpin(obj);
2978+ return -EBADF;
2979+ }
2980+ target_obj_priv = target_obj->driver_private;
2981+
2982+ /* The target buffer should have appeared before us in the
2983+ * exec_object list, so it should have a GTT space bound by now.
2984+ */
2985+ if (target_obj_priv->gtt_space == NULL) {
2986+ DRM_ERROR("No GTT space found for object %d\n",
2987+ reloc.target_handle);
2988+ drm_gem_object_unreference(target_obj);
2989+ i915_gem_object_unpin(obj);
2990+ return -EINVAL;
2991+ }
2992+
2993+ if (reloc.offset > obj->size - 4) {
2994+ DRM_ERROR("Relocation beyond object bounds: "
2995+ "obj %p target %d offset %d size %d.\n",
2996+ obj, reloc.target_handle,
2997+ (int) reloc.offset, (int) obj->size);
2998+ drm_gem_object_unreference(target_obj);
2999+ i915_gem_object_unpin(obj);
3000+ return -EINVAL;
3001+ }
3002+ if (reloc.offset & 3) {
3003+ DRM_ERROR("Relocation not 4-byte aligned: "
3004+ "obj %p target %d offset %d.\n",
3005+ obj, reloc.target_handle,
3006+ (int) reloc.offset);
3007+ drm_gem_object_unreference(target_obj);
3008+ i915_gem_object_unpin(obj);
3009+ return -EINVAL;
3010+ }
3011+
3012+ if (reloc.write_domain && target_obj->pending_write_domain &&
3013+ reloc.write_domain != target_obj->pending_write_domain) {
3014+ DRM_ERROR("Write domain conflict: "
3015+ "obj %p target %d offset %d "
3016+ "new %08x old %08x\n",
3017+ obj, reloc.target_handle,
3018+ (int) reloc.offset,
3019+ reloc.write_domain,
3020+ target_obj->pending_write_domain);
3021+ drm_gem_object_unreference(target_obj);
3022+ i915_gem_object_unpin(obj);
3023+ return -EINVAL;
3024+ }
3025+
3026+#if WATCH_RELOC
3027+ DRM_INFO("%s: obj %p offset %08x target %d "
3028+ "read %08x write %08x gtt %08x "
3029+ "presumed %08x delta %08x\n",
3030+ __func__,
3031+ obj,
3032+ (int) reloc.offset,
3033+ (int) reloc.target_handle,
3034+ (int) reloc.read_domains,
3035+ (int) reloc.write_domain,
3036+ (int) target_obj_priv->gtt_offset,
3037+ (int) reloc.presumed_offset,
3038+ reloc.delta);
3039+#endif
3040+
3041+ target_obj->pending_read_domains |= reloc.read_domains;
3042+ target_obj->pending_write_domain |= reloc.write_domain;
3043+
3044+ /* If the relocation already has the right value in it, no
3045+ * more work needs to be done.
3046+ */
3047+ if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
3048+ drm_gem_object_unreference(target_obj);
3049+ continue;
3050+ }
3051+
3052+ /* Now that we're going to actually write some data in,
3053+ * make sure that any rendering using this buffer's contents
3054+ * is completed.
3055+ */
3056+ i915_gem_object_wait_rendering(obj);
3057+
3058+ /* As we're writing through the gtt, flush
3059+ * any CPU writes before we write the relocations
3060+ */
3061+ if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
3062+ i915_gem_clflush_object(obj);
3063+ drm_agp_chipset_flush(dev);
3064+ obj->write_domain = 0;
3065+ }
3066+
3067+ /* Map the page containing the relocation we're going to
3068+ * perform.
3069+ */
3070+ reloc_offset = obj_priv->gtt_offset + reloc.offset;
3071+ if (reloc_page == NULL ||
3072+ (last_reloc_offset & ~(PAGE_SIZE - 1)) !=
3073+ (reloc_offset & ~(PAGE_SIZE - 1))) {
3074+ if (reloc_page != NULL)
3075+ iounmap(reloc_page);
3076+
3077+ reloc_page = ioremap(dev->agp->base +
3078+ (reloc_offset & ~(PAGE_SIZE - 1)),
3079+ PAGE_SIZE);
3080+ last_reloc_offset = reloc_offset;
3081+ if (reloc_page == NULL) {
3082+ drm_gem_object_unreference(target_obj);
3083+ i915_gem_object_unpin(obj);
3084+ return -ENOMEM;
3085+ }
3086+ }
3087+
3088+ reloc_entry = (uint32_t *)((char *)reloc_page +
3089+ (reloc_offset & (PAGE_SIZE - 1)));
3090+ reloc_val = target_obj_priv->gtt_offset + reloc.delta;
3091+
3092+#if WATCH_BUF
3093+ DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
3094+ obj, (unsigned int) reloc.offset,
3095+ readl(reloc_entry), reloc_val);
3096+#endif
3097+ writel(reloc_val, reloc_entry);
3098+
3099+ /* Write the updated presumed offset for this entry back out
3100+ * to the user.
3101+ */
3102+ reloc.presumed_offset = target_obj_priv->gtt_offset;
3103+ ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
3104+ if (ret != 0) {
3105+ drm_gem_object_unreference(target_obj);
3106+ i915_gem_object_unpin(obj);
3107+ return ret;
3108+ }
3109+
3110+ drm_gem_object_unreference(target_obj);
3111+ }
3112+
3113+ if (reloc_page != NULL)
3114+ iounmap(reloc_page);
3115+
3116+#if WATCH_BUF
3117+ if (0)
3118+ i915_gem_dump_object(obj, 128, __func__, ~0);
3119+#endif
3120+ return 0;
3121+}
3122+
3123+/** Dispatch a batchbuffer to the ring
3124+ */
3125+static int
3126+i915_dispatch_gem_execbuffer(struct drm_device *dev,
3127+ struct drm_i915_gem_execbuffer *exec,
3128+ uint64_t exec_offset)
3129+{
3130+ drm_i915_private_t *dev_priv = dev->dev_private;
3131+ struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
3132+ (uintptr_t) exec->cliprects_ptr;
3133+ int nbox = exec->num_cliprects;
3134+ int i = 0, count;
3135+ uint32_t exec_start, exec_len;
3136+ RING_LOCALS;
3137+
3138+ exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3139+ exec_len = (uint32_t) exec->batch_len;
3140+
3141+ if ((exec_start | exec_len) & 0x7) {
3142+ DRM_ERROR("alignment\n");
3143+ return -EINVAL;
3144+ }
3145+
3146+ if (!exec_start)
3147+ return -EINVAL;
3148+
3149+ count = nbox ? nbox : 1;
3150+
3151+ for (i = 0; i < count; i++) {
3152+ if (i < nbox) {
3153+ int ret = i915_emit_box(dev, boxes, i,
3154+ exec->DR1, exec->DR4);
3155+ if (ret)
3156+ return ret;
3157+ }
3158+
3159+ if (IS_I830(dev) || IS_845G(dev)) {
3160+ BEGIN_LP_RING(4);
3161+ OUT_RING(MI_BATCH_BUFFER);
3162+ OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3163+ OUT_RING(exec_start + exec_len - 4);
3164+ OUT_RING(0);
3165+ ADVANCE_LP_RING();
3166+ } else {
3167+ BEGIN_LP_RING(2);
3168+ if (IS_I965G(dev)) {
3169+ OUT_RING(MI_BATCH_BUFFER_START |
3170+ (2 << 6) |
3171+ MI_BATCH_NON_SECURE_I965);
3172+ OUT_RING(exec_start);
3173+ } else {
3174+ OUT_RING(MI_BATCH_BUFFER_START |
3175+ (2 << 6));
3176+ OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3177+ }
3178+ ADVANCE_LP_RING();
3179+ }
3180+ }
3181+
3182+ /* XXX breadcrumb */
3183+ return 0;
3184+}
3185+
3186+/* Throttle our rendering by waiting until the ring has completed our requests
3187+ * emitted over 20 msec ago.
3188+ *
3189+ * This should get us reasonable parallelism between CPU and GPU but also
3190+ * relatively low latency when blocking on a particular request to finish.
3191+ */
3192+static int
3193+i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3194+{
3195+ struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3196+ int ret = 0;
3197+ uint32_t seqno;
3198+
3199+ mutex_lock(&dev->struct_mutex);
3200+ seqno = i915_file_priv->mm.last_gem_throttle_seqno;
3201+ i915_file_priv->mm.last_gem_throttle_seqno =
3202+ i915_file_priv->mm.last_gem_seqno;
3203+ if (seqno)
3204+ ret = i915_wait_request(dev, seqno);
3205+ mutex_unlock(&dev->struct_mutex);
3206+ return ret;
3207+}
3208+
3209+int
3210+i915_gem_execbuffer(struct drm_device *dev, void *data,
3211+ struct drm_file *file_priv)
3212+{
3213+ drm_i915_private_t *dev_priv = dev->dev_private;
3214+ struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3215+ struct drm_i915_gem_execbuffer *args = data;
3216+ struct drm_i915_gem_exec_object *exec_list = NULL;
3217+ struct drm_gem_object **object_list = NULL;
3218+ struct drm_gem_object *batch_obj;
3219+ int ret, i, pinned = 0;
3220+ uint64_t exec_offset;
3221+ uint32_t seqno, flush_domains, pre_flush_domains;
3222+
3223+#if WATCH_EXEC
3224+ DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3225+ (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3226+#endif
3227+
3228+ /* Copy in the exec list from userland */
3229+ exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
3230+ DRM_MEM_DRIVER);
3231+ object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
3232+ DRM_MEM_DRIVER);
3233+ if (exec_list == NULL || object_list == NULL) {
3234+ DRM_ERROR("Failed to allocate exec or object list "
3235+ "for %d buffers\n",
3236+ args->buffer_count);
3237+ ret = -ENOMEM;
3238+ goto pre_mutex_err;
3239+ }
3240+ ret = copy_from_user(exec_list,
3241+ (struct drm_i915_relocation_entry __user *)
3242+ (uintptr_t) args->buffers_ptr,
3243+ sizeof(*exec_list) * args->buffer_count);
3244+ if (ret != 0) {
3245+ DRM_ERROR("copy %d exec entries failed %d\n",
3246+ args->buffer_count, ret);
3247+ goto pre_mutex_err;
3248+ }
3249+
3250+ mutex_lock(&dev->struct_mutex);
3251+
3252+ i915_verify_inactive(dev, __FILE__, __LINE__);
3253+
3254+ if (dev_priv->mm.wedged) {
3255+ DRM_ERROR("Execbuf while wedged\n");
3256+ mutex_unlock(&dev->struct_mutex);
3257+ return -EIO;
3258+ }
3259+
3260+ if (dev_priv->mm.suspended) {
3261+ DRM_ERROR("Execbuf while VT-switched.\n");
3262+ mutex_unlock(&dev->struct_mutex);
3263+ return -EBUSY;
3264+ }
3265+
3266+ /* Zero the gloabl flush/invalidate flags. These
3267+ * will be modified as each object is bound to the
3268+ * gtt
3269+ */
3270+ dev->invalidate_domains = 0;
3271+ dev->flush_domains = 0;
3272+
3273+ /* Look up object handles and perform the relocations */
3274+ for (i = 0; i < args->buffer_count; i++) {
3275+ object_list[i] = drm_gem_object_lookup(dev, file_priv,
3276+ exec_list[i].handle);
3277+ if (object_list[i] == NULL) {
3278+ DRM_ERROR("Invalid object handle %d at index %d\n",
3279+ exec_list[i].handle, i);
3280+ ret = -EBADF;
3281+ goto err;
3282+ }
3283+
3284+ object_list[i]->pending_read_domains = 0;
3285+ object_list[i]->pending_write_domain = 0;
3286+ ret = i915_gem_object_pin_and_relocate(object_list[i],
3287+ file_priv,
3288+ &exec_list[i]);
3289+ if (ret) {
3290+ DRM_ERROR("object bind and relocate failed %d\n", ret);
3291+ goto err;
3292+ }
3293+ pinned = i + 1;
3294+ }
3295+
3296+ /* Set the pending read domains for the batch buffer to COMMAND */
3297+ batch_obj = object_list[args->buffer_count-1];
3298+ batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
3299+ batch_obj->pending_write_domain = 0;
3300+
3301+ i915_verify_inactive(dev, __FILE__, __LINE__);
3302+
3303+ for (i = 0; i < args->buffer_count; i++) {
3304+ struct drm_gem_object *obj = object_list[i];
3305+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
3306+
3307+ if (obj_priv->gtt_space == NULL) {
3308+ /* We evicted the buffer in the process of validating
3309+ * our set of buffers in. We could try to recover by
3310+ * kicking them everything out and trying again from
3311+ * the start.
3312+ */
3313+ ret = -ENOMEM;
3314+ goto err;
3315+ }
3316+
3317+ /* make sure all previous memory operations have passed */
3318+ ret = i915_gem_object_set_domain(obj,
3319+ obj->pending_read_domains,
3320+ obj->pending_write_domain);
3321+ if (ret)
3322+ goto err;
3323+ }
3324+
3325+ i915_verify_inactive(dev, __FILE__, __LINE__);
3326+
3327+ /* Flush/invalidate caches and chipset buffer */
3328+ flush_domains = i915_gem_dev_set_domain(dev);
3329+
3330+ i915_verify_inactive(dev, __FILE__, __LINE__);
3331+
3332+#if WATCH_COHERENCY
3333+ for (i = 0; i < args->buffer_count; i++) {
3334+ i915_gem_object_check_coherency(object_list[i],
3335+ exec_list[i].handle);
3336+ }
3337+#endif
3338+
3339+ exec_offset = exec_list[args->buffer_count - 1].offset;
3340+
3341+#if WATCH_EXEC
3342+ i915_gem_dump_object(object_list[args->buffer_count - 1],
3343+ args->batch_len,
3344+ __func__,
3345+ ~0);
3346+#endif
3347+
3348+ pre_flush_domains = flush_domains;
3349+
3350+ /* Exec the batchbuffer */
3351+ ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
3352+ if (ret) {
3353+ DRM_ERROR("dispatch failed %d\n", ret);
3354+ goto err;
3355+ }
3356+
3357+ /*
3358+ * Ensure that the commands in the batch buffer are
3359+ * finished before the interrupt fires
3360+ */
3361+ flush_domains |= i915_retire_commands(dev);
3362+
3363+ i915_verify_inactive(dev, __FILE__, __LINE__);
3364+
3365+ /*
3366+ * Get a seqno representing the execution of the current buffer,
3367+ * which we can wait on. We would like to mitigate these interrupts,
3368+ * likely by only creating seqnos occasionally (so that we have
3369+ * *some* interrupts representing completion of buffers that we can
3370+ * wait on when trying to clear up gtt space).
3371+ */
3372+ seqno = i915_add_request(dev, flush_domains);
3373+ BUG_ON(seqno == 0);
3374+ i915_file_priv->mm.last_gem_seqno = seqno;
3375+ for (i = 0; i < args->buffer_count; i++) {
3376+ struct drm_gem_object *obj = object_list[i];
3377+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
3378+
3379+ if (pre_flush_domains & obj->pending_write_domain &
3380+ ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) {
3381+ /* If we had a batchbuffer that resulted in a GPU
3382+ * domain being flushed before execution, followed by
3383+ * execution that resulted in the write_domain being
3384+ * set, then when that request is retired the
3385+ * write_domain would be incorrectly cleared. We're not
3386+ * sure that this can be triggered.
3387+ */
3388+ DRM_ERROR("Going to lose the write domain on "
3389+ "obj %d size %d\n",
3390+ exec_list[i].handle, obj->size);
3391+ }
3392+
3393+ i915_gem_object_move_to_active(obj);
3394+ obj_priv->last_rendering_seqno = seqno;
3395+#if WATCH_LRU
3396+ DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3397+#endif
3398+ }
3399+#if WATCH_LRU
3400+ i915_dump_lru(dev, __func__);
3401+#endif
3402+
3403+ i915_verify_inactive(dev, __FILE__, __LINE__);
3404+
3405+ /* Copy the new buffer offsets back to the user's exec list. */
3406+ ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3407+ (uintptr_t) args->buffers_ptr,
3408+ exec_list,
3409+ sizeof(*exec_list) * args->buffer_count);
3410+ if (ret)
3411+ DRM_ERROR("failed to copy %d exec entries "
3412+ "back to user (%d)\n",
3413+ args->buffer_count, ret);
3414+err:
3415+ if (object_list != NULL) {
3416+ for (i = 0; i < pinned; i++)
3417+ i915_gem_object_unpin(object_list[i]);
3418+
3419+ for (i = 0; i < args->buffer_count; i++)
3420+ drm_gem_object_unreference(object_list[i]);
3421+ }
3422+ mutex_unlock(&dev->struct_mutex);
3423+
3424+pre_mutex_err:
3425+ drm_free(object_list, sizeof(*object_list) * args->buffer_count,
3426+ DRM_MEM_DRIVER);
3427+ drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
3428+ DRM_MEM_DRIVER);
3429+
3430+ return ret;
3431+}
3432+
3433+int
3434+i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3435+{
3436+ struct drm_device *dev = obj->dev;
3437+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
3438+ int ret;
3439+
3440+ i915_verify_inactive(dev, __FILE__, __LINE__);
3441+ if (obj_priv->gtt_space == NULL) {
3442+ ret = i915_gem_object_bind_to_gtt(obj, alignment);
3443+ if (ret != 0) {
3444+ DRM_ERROR("Failure to bind: %d", ret);
3445+ return ret;
3446+ }
3447+ }
3448+ obj_priv->pin_count++;
3449+
3450+ /* If the object is not active and not pending a flush,
3451+ * remove it from the inactive list
3452+ */
3453+ if (obj_priv->pin_count == 1) {
3454+ atomic_inc(&dev->pin_count);
3455+ atomic_add(obj->size, &dev->pin_memory);
3456+ if (!obj_priv->active &&
3457+ (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3458+ I915_GEM_DOMAIN_GTT)) == 0 &&
3459+ !list_empty(&obj_priv->list))
3460+ list_del_init(&obj_priv->list);
3461+ }
3462+ i915_verify_inactive(dev, __FILE__, __LINE__);
3463+
3464+ return 0;
3465+}
3466+
3467+void
3468+i915_gem_object_unpin(struct drm_gem_object *obj)
3469+{
3470+ struct drm_device *dev = obj->dev;
3471+ drm_i915_private_t *dev_priv = dev->dev_private;
3472+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
3473+
3474+ i915_verify_inactive(dev, __FILE__, __LINE__);
3475+ obj_priv->pin_count--;
3476+ BUG_ON(obj_priv->pin_count < 0);
3477+ BUG_ON(obj_priv->gtt_space == NULL);
3478+
3479+ /* If the object is no longer pinned, and is
3480+ * neither active nor being flushed, then stick it on
3481+ * the inactive list
3482+ */
3483+ if (obj_priv->pin_count == 0) {
3484+ if (!obj_priv->active &&
3485+ (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3486+ I915_GEM_DOMAIN_GTT)) == 0)
3487+ list_move_tail(&obj_priv->list,
3488+ &dev_priv->mm.inactive_list);
3489+ atomic_dec(&dev->pin_count);
3490+ atomic_sub(obj->size, &dev->pin_memory);
3491+ }
3492+ i915_verify_inactive(dev, __FILE__, __LINE__);
3493+}
3494+
3495+int
3496+i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3497+ struct drm_file *file_priv)
3498+{
3499+ struct drm_i915_gem_pin *args = data;
3500+ struct drm_gem_object *obj;
3501+ struct drm_i915_gem_object *obj_priv;
3502+ int ret;
3503+
3504+ mutex_lock(&dev->struct_mutex);
3505+
3506+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3507+ if (obj == NULL) {
3508+ DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3509+ args->handle);
3510+ mutex_unlock(&dev->struct_mutex);
3511+ return -EBADF;
3512+ }
3513+ obj_priv = obj->driver_private;
3514+
3515+ ret = i915_gem_object_pin(obj, args->alignment);
3516+ if (ret != 0) {
3517+ drm_gem_object_unreference(obj);
3518+ mutex_unlock(&dev->struct_mutex);
3519+ return ret;
3520+ }
3521+
3522+ /* XXX - flush the CPU caches for pinned objects
3523+ * as the X server doesn't manage domains yet
3524+ */
3525+ if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
3526+ i915_gem_clflush_object(obj);
3527+ drm_agp_chipset_flush(dev);
3528+ obj->write_domain = 0;
3529+ }
3530+ args->offset = obj_priv->gtt_offset;
3531+ drm_gem_object_unreference(obj);
3532+ mutex_unlock(&dev->struct_mutex);
3533+
3534+ return 0;
3535+}
3536+
3537+int
3538+i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3539+ struct drm_file *file_priv)
3540+{
3541+ struct drm_i915_gem_pin *args = data;
3542+ struct drm_gem_object *obj;
3543+
3544+ mutex_lock(&dev->struct_mutex);
3545+
3546+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3547+ if (obj == NULL) {
3548+ DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3549+ args->handle);
3550+ mutex_unlock(&dev->struct_mutex);
3551+ return -EBADF;
3552+ }
3553+
3554+ i915_gem_object_unpin(obj);
3555+
3556+ drm_gem_object_unreference(obj);
3557+ mutex_unlock(&dev->struct_mutex);
3558+ return 0;
3559+}
3560+
3561+int
3562+i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3563+ struct drm_file *file_priv)
3564+{
3565+ struct drm_i915_gem_busy *args = data;
3566+ struct drm_gem_object *obj;
3567+ struct drm_i915_gem_object *obj_priv;
3568+
3569+ mutex_lock(&dev->struct_mutex);
3570+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3571+ if (obj == NULL) {
3572+ DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3573+ args->handle);
3574+ mutex_unlock(&dev->struct_mutex);
3575+ return -EBADF;
3576+ }
3577+
3578+ obj_priv = obj->driver_private;
3579+ args->busy = obj_priv->active;
3580+
3581+ drm_gem_object_unreference(obj);
3582+ mutex_unlock(&dev->struct_mutex);
3583+ return 0;
3584+}
3585+
3586+int
3587+i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3588+ struct drm_file *file_priv)
3589+{
3590+ return i915_gem_ring_throttle(dev, file_priv);
3591+}
3592+
3593+int i915_gem_init_object(struct drm_gem_object *obj)
3594+{
3595+ struct drm_i915_gem_object *obj_priv;
3596+
3597+ obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
3598+ if (obj_priv == NULL)
3599+ return -ENOMEM;
3600+
3601+ /*
3602+ * We've just allocated pages from the kernel,
3603+ * so they've just been written by the CPU with
3604+ * zeros. They'll need to be clflushed before we
3605+ * use them with the GPU.
3606+ */
3607+ obj->write_domain = I915_GEM_DOMAIN_CPU;
3608+ obj->read_domains = I915_GEM_DOMAIN_CPU;
3609+
3610+ obj->driver_private = obj_priv;
3611+ obj_priv->obj = obj;
3612+ INIT_LIST_HEAD(&obj_priv->list);
3613+ return 0;
3614+}
3615+
3616+void i915_gem_free_object(struct drm_gem_object *obj)
3617+{
3618+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
3619+
3620+ while (obj_priv->pin_count > 0)
3621+ i915_gem_object_unpin(obj);
3622+
3623+ i915_gem_object_unbind(obj);
3624+
3625+ drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
3626+ drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
3627+}
3628+
3629+static int
3630+i915_gem_set_domain(struct drm_gem_object *obj,
3631+ struct drm_file *file_priv,
3632+ uint32_t read_domains,
3633+ uint32_t write_domain)
3634+{
3635+ struct drm_device *dev = obj->dev;
3636+ int ret;
3637+ uint32_t flush_domains;
3638+
3639+ BUG_ON(!mutex_is_locked(&dev->struct_mutex));
3640+
3641+ ret = i915_gem_object_set_domain(obj, read_domains, write_domain);
3642+ if (ret)
3643+ return ret;
3644+ flush_domains = i915_gem_dev_set_domain(obj->dev);
3645+
3646+ if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))
3647+ (void) i915_add_request(dev, flush_domains);
3648+
3649+ return 0;
3650+}
3651+
3652+/** Unbinds all objects that are on the given buffer list. */
3653+static int
3654+i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3655+{
3656+ struct drm_gem_object *obj;
3657+ struct drm_i915_gem_object *obj_priv;
3658+ int ret;
3659+
3660+ while (!list_empty(head)) {
3661+ obj_priv = list_first_entry(head,
3662+ struct drm_i915_gem_object,
3663+ list);
3664+ obj = obj_priv->obj;
3665+
3666+ if (obj_priv->pin_count != 0) {
3667+ DRM_ERROR("Pinned object in unbind list\n");
3668+ mutex_unlock(&dev->struct_mutex);
3669+ return -EINVAL;
3670+ }
3671+
3672+ ret = i915_gem_object_unbind(obj);
3673+ if (ret != 0) {
3674+ DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3675+ ret);
3676+ mutex_unlock(&dev->struct_mutex);
3677+ return ret;
3678+ }
3679+ }
3680+
3681+
3682+ return 0;
3683+}
3684+
3685+static int
3686+i915_gem_idle(struct drm_device *dev)
3687+{
3688+ drm_i915_private_t *dev_priv = dev->dev_private;
3689+ uint32_t seqno, cur_seqno, last_seqno;
3690+ int stuck;
3691+
3692+ if (dev_priv->mm.suspended)
3693+ return 0;
3694+
3695+ /* Hack! Don't let anybody do execbuf while we don't control the chip.
3696+ * We need to replace this with a semaphore, or something.
3697+ */
3698+ dev_priv->mm.suspended = 1;
3699+
3700+ i915_kernel_lost_context(dev);
3701+
3702+ /* Flush the GPU along with all non-CPU write domains
3703+ */
3704+ i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
3705+ ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
3706+ seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
3707+ I915_GEM_DOMAIN_GTT));
3708+
3709+ if (seqno == 0) {
3710+ mutex_unlock(&dev->struct_mutex);
3711+ return -ENOMEM;
3712+ }
3713+
3714+ dev_priv->mm.waiting_gem_seqno = seqno;
3715+ last_seqno = 0;
3716+ stuck = 0;
3717+ for (;;) {
3718+ cur_seqno = i915_get_gem_seqno(dev);
3719+ if (i915_seqno_passed(cur_seqno, seqno))
3720+ break;
3721+ if (last_seqno == cur_seqno) {
3722+ if (stuck++ > 100) {
3723+ DRM_ERROR("hardware wedged\n");
3724+ dev_priv->mm.wedged = 1;
3725+ DRM_WAKEUP(&dev_priv->irq_queue);
3726+ break;
3727+ }
3728+ }
3729+ msleep(10);
3730+ last_seqno = cur_seqno;
3731+ }
3732+ dev_priv->mm.waiting_gem_seqno = 0;
3733+
3734+ i915_gem_retire_requests(dev);
3735+
3736+ /* Active and flushing should now be empty as we've
3737+ * waited for a sequence higher than any pending execbuffer
3738+ */
3739+ BUG_ON(!list_empty(&dev_priv->mm.active_list));
3740+ BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3741+
3742+ /* Request should now be empty as we've also waited
3743+ * for the last request in the list
3744+ */
3745+ BUG_ON(!list_empty(&dev_priv->mm.request_list));
3746+
3747+ /* Move all buffers out of the GTT. */
3748+ i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
3749+
3750+ BUG_ON(!list_empty(&dev_priv->mm.active_list));
3751+ BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3752+ BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3753+ BUG_ON(!list_empty(&dev_priv->mm.request_list));
3754+ return 0;
3755+}
3756+
3757+static int
3758+i915_gem_init_hws(struct drm_device *dev)
3759+{
3760+ drm_i915_private_t *dev_priv = dev->dev_private;
3761+ struct drm_gem_object *obj;
3762+ struct drm_i915_gem_object *obj_priv;
3763+ int ret;
3764+
3765+ /* If we need a physical address for the status page, it's already
3766+ * initialized at driver load time.
3767+ */
3768+ if (!I915_NEED_GFX_HWS(dev))
3769+ return 0;
3770+
3771+ obj = drm_gem_object_alloc(dev, 4096);
3772+ if (obj == NULL) {
3773+ DRM_ERROR("Failed to allocate status page\n");
3774+ return -ENOMEM;
3775+ }
3776+ obj_priv = obj->driver_private;
3777+
3778+ ret = i915_gem_object_pin(obj, 4096);
3779+ if (ret != 0) {
3780+ drm_gem_object_unreference(obj);
3781+ return ret;
3782+ }
3783+
3784+ dev_priv->status_gfx_addr = obj_priv->gtt_offset;
3785+ dev_priv->hws_map.offset = dev->agp->base + obj_priv->gtt_offset;
3786+ dev_priv->hws_map.size = 4096;
3787+ dev_priv->hws_map.type = 0;
3788+ dev_priv->hws_map.flags = 0;
3789+ dev_priv->hws_map.mtrr = 0;
3790+
3791+ drm_core_ioremap(&dev_priv->hws_map, dev);
3792+ if (dev_priv->hws_map.handle == NULL) {
3793+ DRM_ERROR("Failed to map status page.\n");
3794+ memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3795+ drm_gem_object_unreference(obj);
3796+ return -EINVAL;
3797+ }
3798+ dev_priv->hws_obj = obj;
3799+ dev_priv->hw_status_page = dev_priv->hws_map.handle;
3800+ memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3801+ I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
3802+ DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3803+
3804+ return 0;
3805+}
3806+
3807+static int
3808+i915_gem_init_ringbuffer(struct drm_device *dev)
3809+{
3810+ drm_i915_private_t *dev_priv = dev->dev_private;
3811+ struct drm_gem_object *obj;
3812+ struct drm_i915_gem_object *obj_priv;
3813+ int ret;
3814+
3815+ ret = i915_gem_init_hws(dev);
3816+ if (ret != 0)
3817+ return ret;
3818+
3819+ obj = drm_gem_object_alloc(dev, 128 * 1024);
3820+ if (obj == NULL) {
3821+ DRM_ERROR("Failed to allocate ringbuffer\n");
3822+ return -ENOMEM;
3823+ }
3824+ obj_priv = obj->driver_private;
3825+
3826+ ret = i915_gem_object_pin(obj, 4096);
3827+ if (ret != 0) {
3828+ drm_gem_object_unreference(obj);
3829+ return ret;
3830+ }
3831+
3832+ /* Set up the kernel mapping for the ring. */
3833+ dev_priv->ring.Size = obj->size;
3834+ dev_priv->ring.tail_mask = obj->size - 1;
3835+
3836+ dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
3837+ dev_priv->ring.map.size = obj->size;
3838+ dev_priv->ring.map.type = 0;
3839+ dev_priv->ring.map.flags = 0;
3840+ dev_priv->ring.map.mtrr = 0;
3841+
3842+ drm_core_ioremap(&dev_priv->ring.map, dev);
3843+ if (dev_priv->ring.map.handle == NULL) {
3844+ DRM_ERROR("Failed to map ringbuffer.\n");
3845+ memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3846+ drm_gem_object_unreference(obj);
3847+ return -EINVAL;
3848+ }
3849+ dev_priv->ring.ring_obj = obj;
3850+ dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
3851+
3852+ /* Stop the ring if it's running. */
3853+ I915_WRITE(PRB0_CTL, 0);
3854+ I915_WRITE(PRB0_HEAD, 0);
3855+ I915_WRITE(PRB0_TAIL, 0);
3856+ I915_WRITE(PRB0_START, 0);
3857+
3858+ /* Initialize the ring. */
3859+ I915_WRITE(PRB0_START, obj_priv->gtt_offset);
3860+ I915_WRITE(PRB0_CTL,
3861+ ((obj->size - 4096) & RING_NR_PAGES) |
3862+ RING_NO_REPORT |
3863+ RING_VALID);
3864+
3865+ /* Update our cache of the ring state */
3866+ i915_kernel_lost_context(dev);
3867+
3868+ return 0;
3869+}
3870+
3871+static void
3872+i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3873+{
3874+ drm_i915_private_t *dev_priv = dev->dev_private;
3875+
3876+ if (dev_priv->ring.ring_obj == NULL)
3877+ return;
3878+
3879+ drm_core_ioremapfree(&dev_priv->ring.map, dev);
3880+
3881+ i915_gem_object_unpin(dev_priv->ring.ring_obj);
3882+ drm_gem_object_unreference(dev_priv->ring.ring_obj);
3883+ dev_priv->ring.ring_obj = NULL;
3884+ memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3885+
3886+ if (dev_priv->hws_obj != NULL) {
3887+ i915_gem_object_unpin(dev_priv->hws_obj);
3888+ drm_gem_object_unreference(dev_priv->hws_obj);
3889+ dev_priv->hws_obj = NULL;
3890+ memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3891+
3892+ /* Write high address into HWS_PGA when disabling. */
3893+ I915_WRITE(HWS_PGA, 0x1ffff000);
3894+ }
3895+}
3896+
3897+int
3898+i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3899+ struct drm_file *file_priv)
3900+{
3901+ drm_i915_private_t *dev_priv = dev->dev_private;
3902+ int ret;
3903+
3904+ if (dev_priv->mm.wedged) {
3905+ DRM_ERROR("Reenabling wedged hardware, good luck\n");
3906+ dev_priv->mm.wedged = 0;
3907+ }
3908+
3909+ ret = i915_gem_init_ringbuffer(dev);
3910+ if (ret != 0)
3911+ return ret;
3912+
3913+ mutex_lock(&dev->struct_mutex);
3914+ BUG_ON(!list_empty(&dev_priv->mm.active_list));
3915+ BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3916+ BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3917+ BUG_ON(!list_empty(&dev_priv->mm.request_list));
3918+ dev_priv->mm.suspended = 0;
3919+ mutex_unlock(&dev->struct_mutex);
3920+ return 0;
3921+}
3922+
3923+int
3924+i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3925+ struct drm_file *file_priv)
3926+{
3927+ int ret;
3928+
3929+ mutex_lock(&dev->struct_mutex);
3930+ ret = i915_gem_idle(dev);
3931+ if (ret == 0)
3932+ i915_gem_cleanup_ringbuffer(dev);
3933+ mutex_unlock(&dev->struct_mutex);
3934+
3935+ return 0;
3936+}
3937+
3938+void
3939+i915_gem_lastclose(struct drm_device *dev)
3940+{
3941+ int ret;
3942+ drm_i915_private_t *dev_priv = dev->dev_private;
3943+
3944+ mutex_lock(&dev->struct_mutex);
3945+
3946+ if (dev_priv->ring.ring_obj != NULL) {
3947+ ret = i915_gem_idle(dev);
3948+ if (ret)
3949+ DRM_ERROR("failed to idle hardware: %d\n", ret);
3950+
3951+ i915_gem_cleanup_ringbuffer(dev);
3952+ }
3953+
3954+ mutex_unlock(&dev->struct_mutex);
3955+}
3956+
3957+void
3958+i915_gem_load(struct drm_device *dev)
3959+{
3960+ drm_i915_private_t *dev_priv = dev->dev_private;
3961+
3962+ INIT_LIST_HEAD(&dev_priv->mm.active_list);
3963+ INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3964+ INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3965+ INIT_LIST_HEAD(&dev_priv->mm.request_list);
3966+ INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3967+ i915_gem_retire_work_handler);
3968+ dev_priv->mm.next_gem_seqno = 1;
3969+
3970+ i915_gem_detect_bit_6_swizzle(dev);
3971+}
3972--- /dev/null
3973+++ b/drivers/gpu/drm/i915/i915_gem_debug.c
3974@@ -0,0 +1,201 @@
3975+/*
3976+ * Copyright © 2008 Intel Corporation
3977+ *
3978+ * Permission is hereby granted, free of charge, to any person obtaining a
3979+ * copy of this software and associated documentation files (the "Software"),
3980+ * to deal in the Software without restriction, including without limitation
3981+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
3982+ * and/or sell copies of the Software, and to permit persons to whom the
3983+ * Software is furnished to do so, subject to the following conditions:
3984+ *
3985+ * The above copyright notice and this permission notice (including the next
3986+ * paragraph) shall be included in all copies or substantial portions of the
3987+ * Software.
3988+ *
3989+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3990+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3991+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
3992+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3993+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
3994+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
3995+ * IN THE SOFTWARE.
3996+ *
3997+ * Authors:
3998+ * Keith Packard <keithp@keithp.com>
3999+ *
4000+ */
4001+
4002+#include "drmP.h"
4003+#include "drm.h"
4004+#include "i915_drm.h"
4005+#include "i915_drv.h"
4006+
4007+#if WATCH_INACTIVE
4008+void
4009+i915_verify_inactive(struct drm_device *dev, char *file, int line)
4010+{
4011+ drm_i915_private_t *dev_priv = dev->dev_private;
4012+ struct drm_gem_object *obj;
4013+ struct drm_i915_gem_object *obj_priv;
4014+
4015+ list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
4016+ obj = obj_priv->obj;
4017+ if (obj_priv->pin_count || obj_priv->active ||
4018+ (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
4019+ I915_GEM_DOMAIN_GTT)))
4020+ DRM_ERROR("inactive %p (p %d a %d w %x) %s:%d\n",
4021+ obj,
4022+ obj_priv->pin_count, obj_priv->active,
4023+ obj->write_domain, file, line);
4024+ }
4025+}
4026+#endif /* WATCH_INACTIVE */
4027+
4028+
4029+#if WATCH_BUF | WATCH_EXEC | WATCH_PWRITE
4030+static void
4031+i915_gem_dump_page(struct page *page, uint32_t start, uint32_t end,
4032+ uint32_t bias, uint32_t mark)
4033+{
4034+ uint32_t *mem = kmap_atomic(page, KM_USER0);
4035+ int i;
4036+ for (i = start; i < end; i += 4)
4037+ DRM_INFO("%08x: %08x%s\n",
4038+ (int) (bias + i), mem[i / 4],
4039+ (bias + i == mark) ? " ********" : "");
4040+ kunmap_atomic(mem, KM_USER0);
4041+ /* give syslog time to catch up */
4042+ msleep(1);
4043+}
4044+
4045+void
4046+i915_gem_dump_object(struct drm_gem_object *obj, int len,
4047+ const char *where, uint32_t mark)
4048+{
4049+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
4050+ int page;
4051+
4052+ DRM_INFO("%s: object at offset %08x\n", where, obj_priv->gtt_offset);
4053+ for (page = 0; page < (len + PAGE_SIZE-1) / PAGE_SIZE; page++) {
4054+ int page_len, chunk, chunk_len;
4055+
4056+ page_len = len - page * PAGE_SIZE;
4057+ if (page_len > PAGE_SIZE)
4058+ page_len = PAGE_SIZE;
4059+
4060+ for (chunk = 0; chunk < page_len; chunk += 128) {
4061+ chunk_len = page_len - chunk;
4062+ if (chunk_len > 128)
4063+ chunk_len = 128;
4064+ i915_gem_dump_page(obj_priv->page_list[page],
4065+ chunk, chunk + chunk_len,
4066+ obj_priv->gtt_offset +
4067+ page * PAGE_SIZE,
4068+ mark);
4069+ }
4070+ }
4071+}
4072+#endif
4073+
4074+#if WATCH_LRU
4075+void
4076+i915_dump_lru(struct drm_device *dev, const char *where)
4077+{
4078+ drm_i915_private_t *dev_priv = dev->dev_private;
4079+ struct drm_i915_gem_object *obj_priv;
4080+
4081+ DRM_INFO("active list %s {\n", where);
4082+ list_for_each_entry(obj_priv, &dev_priv->mm.active_list,
4083+ list)
4084+ {
4085+ DRM_INFO(" %p: %08x\n", obj_priv,
4086+ obj_priv->last_rendering_seqno);
4087+ }
4088+ DRM_INFO("}\n");
4089+ DRM_INFO("flushing list %s {\n", where);
4090+ list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list,
4091+ list)
4092+ {
4093+ DRM_INFO(" %p: %08x\n", obj_priv,
4094+ obj_priv->last_rendering_seqno);
4095+ }
4096+ DRM_INFO("}\n");
4097+ DRM_INFO("inactive %s {\n", where);
4098+ list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
4099+ DRM_INFO(" %p: %08x\n", obj_priv,
4100+ obj_priv->last_rendering_seqno);
4101+ }
4102+ DRM_INFO("}\n");
4103+}
4104+#endif
4105+
4106+
4107+#if WATCH_COHERENCY
4108+void
4109+i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle)
4110+{
4111+ struct drm_device *dev = obj->dev;
4112+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
4113+ int page;
4114+ uint32_t *gtt_mapping;
4115+ uint32_t *backing_map = NULL;
4116+ int bad_count = 0;
4117+
4118+ DRM_INFO("%s: checking coherency of object %p@0x%08x (%d, %dkb):\n",
4119+ __func__, obj, obj_priv->gtt_offset, handle,
4120+ obj->size / 1024);
4121+
4122+ gtt_mapping = ioremap(dev->agp->base + obj_priv->gtt_offset,
4123+ obj->size);
4124+ if (gtt_mapping == NULL) {
4125+ DRM_ERROR("failed to map GTT space\n");
4126+ return;
4127+ }
4128+
4129+ for (page = 0; page < obj->size / PAGE_SIZE; page++) {
4130+ int i;
4131+
4132+ backing_map = kmap_atomic(obj_priv->page_list[page], KM_USER0);
4133+
4134+ if (backing_map == NULL) {
4135+ DRM_ERROR("failed to map backing page\n");
4136+ goto out;
4137+ }
4138+
4139+ for (i = 0; i < PAGE_SIZE / 4; i++) {
4140+ uint32_t cpuval = backing_map[i];
4141+ uint32_t gttval = readl(gtt_mapping +
4142+ page * 1024 + i);
4143+
4144+ if (cpuval != gttval) {
4145+ DRM_INFO("incoherent CPU vs GPU at 0x%08x: "
4146+ "0x%08x vs 0x%08x\n",
4147+ (int)(obj_priv->gtt_offset +
4148+ page * PAGE_SIZE + i * 4),
4149+ cpuval, gttval);
4150+ if (bad_count++ >= 8) {
4151+ DRM_INFO("...\n");
4152+ goto out;
4153+ }
4154+ }
4155+ }
4156+ kunmap_atomic(backing_map, KM_USER0);
4157+ backing_map = NULL;
4158+ }
4159+
4160+ out:
4161+ if (backing_map != NULL)
4162+ kunmap_atomic(backing_map, KM_USER0);
4163+ iounmap(gtt_mapping);
4164+
4165+ /* give syslog time to catch up */
4166+ msleep(1);
4167+
4168+ /* Directly flush the object, since we just loaded values with the CPU
4169+ * from the backing pages and we don't want to disturb the cache
4170+ * management that we're trying to observe.
4171+ */
4172+
4173+ i915_gem_clflush_object(obj);
4174+}
4175+#endif
4176--- /dev/null
4177+++ b/drivers/gpu/drm/i915/i915_gem_proc.c
4178@@ -0,0 +1,292 @@
4179+/*
4180+ * Copyright © 2008 Intel Corporation
4181+ *
4182+ * Permission is hereby granted, free of charge, to any person obtaining a
4183+ * copy of this software and associated documentation files (the "Software"),
4184+ * to deal in the Software without restriction, including without limitation
4185+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
4186+ * and/or sell copies of the Software, and to permit persons to whom the
4187+ * Software is furnished to do so, subject to the following conditions:
4188+ *
4189+ * The above copyright notice and this permission notice (including the next
4190+ * paragraph) shall be included in all copies or substantial portions of the
4191+ * Software.
4192+ *
4193+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4194+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4195+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
4196+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4197+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
4198+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
4199+ * IN THE SOFTWARE.
4200+ *
4201+ * Authors:
4202+ * Eric Anholt <eric@anholt.net>
4203+ * Keith Packard <keithp@keithp.com>
4204+ *
4205+ */
4206+
4207+#include "drmP.h"
4208+#include "drm.h"
4209+#include "i915_drm.h"
4210+#include "i915_drv.h"
4211+
4212+static int i915_gem_active_info(char *buf, char **start, off_t offset,
4213+ int request, int *eof, void *data)
4214+{
4215+ struct drm_minor *minor = (struct drm_minor *) data;
4216+ struct drm_device *dev = minor->dev;
4217+ drm_i915_private_t *dev_priv = dev->dev_private;
4218+ struct drm_i915_gem_object *obj_priv;
4219+ int len = 0;
4220+
4221+ if (offset > DRM_PROC_LIMIT) {
4222+ *eof = 1;
4223+ return 0;
4224+ }
4225+
4226+ *start = &buf[offset];
4227+ *eof = 0;
4228+ DRM_PROC_PRINT("Active:\n");
4229+ list_for_each_entry(obj_priv, &dev_priv->mm.active_list,
4230+ list)
4231+ {
4232+ struct drm_gem_object *obj = obj_priv->obj;
4233+ if (obj->name) {
4234+ DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n",
4235+ obj, obj->name,
4236+ obj->read_domains, obj->write_domain,
4237+ obj_priv->last_rendering_seqno);
4238+ } else {
4239+ DRM_PROC_PRINT(" %p: %08x %08x %d\n",
4240+ obj,
4241+ obj->read_domains, obj->write_domain,
4242+ obj_priv->last_rendering_seqno);
4243+ }
4244+ }
4245+ if (len > request + offset)
4246+ return request;
4247+ *eof = 1;
4248+ return len - offset;
4249+}
4250+
4251+static int i915_gem_flushing_info(char *buf, char **start, off_t offset,
4252+ int request, int *eof, void *data)
4253+{
4254+ struct drm_minor *minor = (struct drm_minor *) data;
4255+ struct drm_device *dev = minor->dev;
4256+ drm_i915_private_t *dev_priv = dev->dev_private;
4257+ struct drm_i915_gem_object *obj_priv;
4258+ int len = 0;
4259+
4260+ if (offset > DRM_PROC_LIMIT) {
4261+ *eof = 1;
4262+ return 0;
4263+ }
4264+
4265+ *start = &buf[offset];
4266+ *eof = 0;
4267+ DRM_PROC_PRINT("Flushing:\n");
4268+ list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list,
4269+ list)
4270+ {
4271+ struct drm_gem_object *obj = obj_priv->obj;
4272+ if (obj->name) {
4273+ DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n",
4274+ obj, obj->name,
4275+ obj->read_domains, obj->write_domain,
4276+ obj_priv->last_rendering_seqno);
4277+ } else {
4278+ DRM_PROC_PRINT(" %p: %08x %08x %d\n", obj,
4279+ obj->read_domains, obj->write_domain,
4280+ obj_priv->last_rendering_seqno);
4281+ }
4282+ }
4283+ if (len > request + offset)
4284+ return request;
4285+ *eof = 1;
4286+ return len - offset;
4287+}
4288+
4289+static int i915_gem_inactive_info(char *buf, char **start, off_t offset,
4290+ int request, int *eof, void *data)
4291+{
4292+ struct drm_minor *minor = (struct drm_minor *) data;
4293+ struct drm_device *dev = minor->dev;
4294+ drm_i915_private_t *dev_priv = dev->dev_private;
4295+ struct drm_i915_gem_object *obj_priv;
4296+ int len = 0;
4297+
4298+ if (offset > DRM_PROC_LIMIT) {
4299+ *eof = 1;
4300+ return 0;
4301+ }
4302+
4303+ *start = &buf[offset];
4304+ *eof = 0;
4305+ DRM_PROC_PRINT("Inactive:\n");
4306+ list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list,
4307+ list)
4308+ {
4309+ struct drm_gem_object *obj = obj_priv->obj;
4310+ if (obj->name) {
4311+ DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n",
4312+ obj, obj->name,
4313+ obj->read_domains, obj->write_domain,
4314+ obj_priv->last_rendering_seqno);
4315+ } else {
4316+ DRM_PROC_PRINT(" %p: %08x %08x %d\n", obj,
4317+ obj->read_domains, obj->write_domain,
4318+ obj_priv->last_rendering_seqno);
4319+ }
4320+ }
4321+ if (len > request + offset)
4322+ return request;
4323+ *eof = 1;
4324+ return len - offset;
4325+}
4326+
4327+static int i915_gem_request_info(char *buf, char **start, off_t offset,
4328+ int request, int *eof, void *data)
4329+{
4330+ struct drm_minor *minor = (struct drm_minor *) data;
4331+ struct drm_device *dev = minor->dev;
4332+ drm_i915_private_t *dev_priv = dev->dev_private;
4333+ struct drm_i915_gem_request *gem_request;
4334+ int len = 0;
4335+
4336+ if (offset > DRM_PROC_LIMIT) {
4337+ *eof = 1;
4338+ return 0;
4339+ }
4340+
4341+ *start = &buf[offset];
4342+ *eof = 0;
4343+ DRM_PROC_PRINT("Request:\n");
4344+ list_for_each_entry(gem_request, &dev_priv->mm.request_list,
4345+ list)
4346+ {
4347+ DRM_PROC_PRINT(" %d @ %d %08x\n",
4348+ gem_request->seqno,
4349+ (int) (jiffies - gem_request->emitted_jiffies),
4350+ gem_request->flush_domains);
4351+ }
4352+ if (len > request + offset)
4353+ return request;
4354+ *eof = 1;
4355+ return len - offset;
4356+}
4357+
4358+static int i915_gem_seqno_info(char *buf, char **start, off_t offset,
4359+ int request, int *eof, void *data)
4360+{
4361+ struct drm_minor *minor = (struct drm_minor *) data;
4362+ struct drm_device *dev = minor->dev;
4363+ drm_i915_private_t *dev_priv = dev->dev_private;
4364+ int len = 0;
4365+
4366+ if (offset > DRM_PROC_LIMIT) {
4367+ *eof = 1;
4368+ return 0;
4369+ }
4370+
4371+ *start = &buf[offset];
4372+ *eof = 0;
4373+ DRM_PROC_PRINT("Current sequence: %d\n", i915_get_gem_seqno(dev));
4374+ DRM_PROC_PRINT("Waiter sequence: %d\n",
4375+ dev_priv->mm.waiting_gem_seqno);
4376+ DRM_PROC_PRINT("IRQ sequence: %d\n", dev_priv->mm.irq_gem_seqno);
4377+ if (len > request + offset)
4378+ return request;
4379+ *eof = 1;
4380+ return len - offset;
4381+}
4382+
4383+
4384+static int i915_interrupt_info(char *buf, char **start, off_t offset,
4385+ int request, int *eof, void *data)
4386+{
4387+ struct drm_minor *minor = (struct drm_minor *) data;
4388+ struct drm_device *dev = minor->dev;
4389+ drm_i915_private_t *dev_priv = dev->dev_private;
4390+ int len = 0;
4391+
4392+ if (offset > DRM_PROC_LIMIT) {
4393+ *eof = 1;
4394+ return 0;
4395+ }
4396+
4397+ *start = &buf[offset];
4398+ *eof = 0;
4399+ DRM_PROC_PRINT("Interrupt enable: %08x\n",
4400+ I915_READ(IER));
4401+ DRM_PROC_PRINT("Interrupt identity: %08x\n",
4402+ I915_READ(IIR));
4403+ DRM_PROC_PRINT("Interrupt mask: %08x\n",
4404+ I915_READ(IMR));
4405+ DRM_PROC_PRINT("Pipe A stat: %08x\n",
4406+ I915_READ(PIPEASTAT));
4407+ DRM_PROC_PRINT("Pipe B stat: %08x\n",
4408+ I915_READ(PIPEBSTAT));
4409+ DRM_PROC_PRINT("Interrupts received: %d\n",
4410+ atomic_read(&dev_priv->irq_received));
4411+ DRM_PROC_PRINT("Current sequence: %d\n",
4412+ i915_get_gem_seqno(dev));
4413+ DRM_PROC_PRINT("Waiter sequence: %d\n",
4414+ dev_priv->mm.waiting_gem_seqno);
4415+ DRM_PROC_PRINT("IRQ sequence: %d\n",
4416+ dev_priv->mm.irq_gem_seqno);
4417+ if (len > request + offset)
4418+ return request;
4419+ *eof = 1;
4420+ return len - offset;
4421+}
4422+
4423+static struct drm_proc_list {
4424+ /** file name */
4425+ const char *name;
4426+ /** proc callback*/
4427+ int (*f) (char *, char **, off_t, int, int *, void *);
4428+} i915_gem_proc_list[] = {
4429+ {"i915_gem_active", i915_gem_active_info},
4430+ {"i915_gem_flushing", i915_gem_flushing_info},
4431+ {"i915_gem_inactive", i915_gem_inactive_info},
4432+ {"i915_gem_request", i915_gem_request_info},
4433+ {"i915_gem_seqno", i915_gem_seqno_info},
4434+ {"i915_gem_interrupt", i915_interrupt_info},
4435+};
4436+
4437+#define I915_GEM_PROC_ENTRIES ARRAY_SIZE(i915_gem_proc_list)
4438+
4439+int i915_gem_proc_init(struct drm_minor *minor)
4440+{
4441+ struct proc_dir_entry *ent;
4442+ int i, j;
4443+
4444+ for (i = 0; i < I915_GEM_PROC_ENTRIES; i++) {
4445+ ent = create_proc_entry(i915_gem_proc_list[i].name,
4446+ S_IFREG | S_IRUGO, minor->dev_root);
4447+ if (!ent) {
4448+ DRM_ERROR("Cannot create /proc/dri/.../%s\n",
4449+ i915_gem_proc_list[i].name);
4450+ for (j = 0; j < i; j++)
4451+ remove_proc_entry(i915_gem_proc_list[i].name,
4452+ minor->dev_root);
4453+ return -1;
4454+ }
4455+ ent->read_proc = i915_gem_proc_list[i].f;
4456+ ent->data = minor;
4457+ }
4458+ return 0;
4459+}
4460+
4461+void i915_gem_proc_cleanup(struct drm_minor *minor)
4462+{
4463+ int i;
4464+
4465+ if (!minor->dev_root)
4466+ return;
4467+
4468+ for (i = 0; i < I915_GEM_PROC_ENTRIES; i++)
4469+ remove_proc_entry(i915_gem_proc_list[i].name, minor->dev_root);
4470+}
4471--- /dev/null
4472+++ b/drivers/gpu/drm/i915/i915_gem_tiling.c
4473@@ -0,0 +1,271 @@
4474+/*
4475+ * Copyright © 2008 Intel Corporation
4476+ *
4477+ * Permission is hereby granted, free of charge, to any person obtaining a
4478+ * copy of this software and associated documentation files (the "Software"),
4479+ * to deal in the Software without restriction, including without limitation
4480+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
4481+ * and/or sell copies of the Software, and to permit persons to whom the
4482+ * Software is furnished to do so, subject to the following conditions:
4483+ *
4484+ * The above copyright notice and this permission notice (including the next
4485+ * paragraph) shall be included in all copies or substantial portions of the
4486+ * Software.
4487+ *
4488+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4489+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4490+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
4491+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4492+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
4493+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
4494+ * IN THE SOFTWARE.
4495+ *
4496+ * Authors:
4497+ * Eric Anholt <eric@anholt.net>
4498+ *
4499+ */
4500+
4501+#include "drmP.h"
4502+#include "drm.h"
4503+#include "i915_drm.h"
4504+#include "i915_drv.h"
4505+
4506+/** @file i915_gem_tiling.c
4507+ *
4508+ * Support for managing tiling state of buffer objects.
4509+ *
4510+ * The idea behind tiling is to increase cache hit rates by rearranging
4511+ * pixel data so that a group of pixel accesses are in the same cacheline.
4512+ * Performance improvement from doing this on the back/depth buffer are on
4513+ * the order of 30%.
4514+ *
4515+ * Intel architectures make this somewhat more complicated, though, by
4516+ * adjustments made to addressing of data when the memory is in interleaved
4517+ * mode (matched pairs of DIMMS) to improve memory bandwidth.
4518+ * For interleaved memory, the CPU sends every sequential 64 bytes
4519+ * to an alternate memory channel so it can get the bandwidth from both.
4520+ *
4521+ * The GPU also rearranges its accesses for increased bandwidth to interleaved
4522+ * memory, and it matches what the CPU does for non-tiled. However, when tiled
4523+ * it does it a little differently, since one walks addresses not just in the
4524+ * X direction but also Y. So, along with alternating channels when bit
4525+ * 6 of the address flips, it also alternates when other bits flip -- Bits 9
4526+ * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
4527+ * are common to both the 915 and 965-class hardware.
4528+ *
4529+ * The CPU also sometimes XORs in higher bits as well, to improve
4530+ * bandwidth doing strided access like we do so frequently in graphics. This
4531+ * is called "Channel XOR Randomization" in the MCH documentation. The result
4532+ * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
4533+ * decode.
4534+ *
4535+ * All of this bit 6 XORing has an effect on our memory management,
4536+ * as we need to make sure that the 3d driver can correctly address object
4537+ * contents.
4538+ *
4539+ * If we don't have interleaved memory, all tiling is safe and no swizzling is
4540+ * required.
4541+ *
4542+ * When bit 17 is XORed in, we simply refuse to tile at all. Bit
4543+ * 17 is not just a page offset, so as we page an objet out and back in,
4544+ * individual pages in it will have different bit 17 addresses, resulting in
4545+ * each 64 bytes being swapped with its neighbor!
4546+ *
4547+ * Otherwise, if interleaved, we have to tell the 3d driver what the address
4548+ * swizzling it needs to do is, since it's writing with the CPU to the pages
4549+ * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
4550+ * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
4551+ * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
4552+ * to match what the GPU expects.
4553+ */
4554+
4555+/**
4556+ * Detects bit 6 swizzling of address lookup between IGD access and CPU
4557+ * access through main memory.
4558+ */
4559+void
4560+i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
4561+{
4562+ drm_i915_private_t *dev_priv = dev->dev_private;
4563+ struct pci_dev *bridge;
4564+ uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
4565+ uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
4566+ int ret;
4567+
4568+ if (IS_I965G(dev) && !IS_I965GM(dev)) {
4569+ uint32_t chdecmisc;
4570+
4571+ /* On the 965, channel interleave appears to be determined by
4572+ * the flex bit. If flex is set, then the ranks (sides of a
4573+ * DIMM) of memory will be "stacked" (physical addresses walk
4574+ * through one rank then move on to the next, flipping channels
4575+ * or not depending on rank configuration). The GPU in this
4576+ * case does exactly the same addressing as the CPU.
4577+ *
4578+ * Unlike the 945, channel randomization based does not
4579+ * appear to be available.
4580+ *
4581+ * XXX: While the G965 doesn't appear to do any interleaving
4582+ * when the DIMMs are not exactly matched, the G4x chipsets
4583+ * might be for "L-shaped" configurations, and will need to be
4584+ * detected.
4585+ *
4586+ * L-shaped configuration:
4587+ *
4588+ * +-----+
4589+ * | |
4590+ * |DIMM2| <-- non-interleaved
4591+ * +-----+
4592+ * +-----+ +-----+
4593+ * | | | |
4594+ * |DIMM0| |DIMM1| <-- interleaved area
4595+ * +-----+ +-----+
4596+ */
4597+ chdecmisc = I915_READ(CHDECMISC);
4598+
4599+ if (chdecmisc == 0xff) {
4600+ DRM_ERROR("Couldn't read from MCHBAR. "
4601+ "Disabling tiling.\n");
4602+ } else if (chdecmisc & CHDECMISC_FLEXMEMORY) {
4603+ swizzle_x = I915_BIT_6_SWIZZLE_NONE;
4604+ swizzle_y = I915_BIT_6_SWIZZLE_NONE;
4605+ } else {
4606+ swizzle_x = I915_BIT_6_SWIZZLE_9_10;
4607+ swizzle_y = I915_BIT_6_SWIZZLE_9;
4608+ }
4609+ } else if (IS_I9XX(dev)) {
4610+ uint32_t dcc;
4611+
4612+ /* On 915-945 and GM965, channel interleave by the CPU is
4613+ * determined by DCC. The CPU will alternate based on bit 6
4614+ * in interleaved mode, and the GPU will then also alternate
4615+ * on bit 6, 9, and 10 for X, but the CPU may also optionally
4616+ * alternate based on bit 17 (XOR not disabled and XOR
4617+ * bit == 17).
4618+ */
4619+ dcc = I915_READ(DCC);
4620+ switch (dcc & DCC_ADDRESSING_MODE_MASK) {
4621+ case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
4622+ case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
4623+ swizzle_x = I915_BIT_6_SWIZZLE_NONE;
4624+ swizzle_y = I915_BIT_6_SWIZZLE_NONE;
4625+ break;
4626+ case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
4627+ if (IS_I915G(dev) || IS_I915GM(dev) ||
4628+ dcc & DCC_CHANNEL_XOR_DISABLE) {
4629+ swizzle_x = I915_BIT_6_SWIZZLE_9_10;
4630+ swizzle_y = I915_BIT_6_SWIZZLE_9;
4631+ } else if (IS_I965GM(dev)) {
4632+ /* GM965 only does bit 11-based channel
4633+ * randomization
4634+ */
4635+ swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
4636+ swizzle_y = I915_BIT_6_SWIZZLE_9_11;
4637+ } else {
4638+ /* Bit 17 or perhaps other swizzling */
4639+ swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
4640+ swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
4641+ }
4642+ break;
4643+ }
4644+ if (dcc == 0xffffffff) {
4645+ DRM_ERROR("Couldn't read from MCHBAR. "
4646+ "Disabling tiling.\n");
4647+ swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
4648+ swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
4649+ }
4650+ } else {
4651+ /* As far as we know, the 865 doesn't have these bit 6
4652+ * swizzling issues.
4653+ */
4654+ swizzle_x = I915_BIT_6_SWIZZLE_NONE;
4655+ swizzle_y = I915_BIT_6_SWIZZLE_NONE;
4656+ }
4657+
4658+ dev_priv->mm.bit_6_swizzle_x = swizzle_x;
4659+ dev_priv->mm.bit_6_swizzle_y = swizzle_y;
4660+}
4661+
4662+/**
4663+ * Sets the tiling mode of an object, returning the required swizzling of
4664+ * bit 6 of addresses in the object.
4665+ */
4666+int
4667+i915_gem_set_tiling(struct drm_device *dev, void *data,
4668+ struct drm_file *file_priv)
4669+{
4670+ struct drm_i915_gem_set_tiling *args = data;
4671+ drm_i915_private_t *dev_priv = dev->dev_private;
4672+ struct drm_gem_object *obj;
4673+ struct drm_i915_gem_object *obj_priv;
4674+
4675+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4676+ if (obj == NULL)
4677+ return -EINVAL;
4678+ obj_priv = obj->driver_private;
4679+
4680+ mutex_lock(&dev->struct_mutex);
4681+
4682+ if (args->tiling_mode == I915_TILING_NONE) {
4683+ obj_priv->tiling_mode = I915_TILING_NONE;
4684+ args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
4685+ } else {
4686+ if (args->tiling_mode == I915_TILING_X)
4687+ args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
4688+ else
4689+ args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
4690+ /* If we can't handle the swizzling, make it untiled. */
4691+ if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
4692+ args->tiling_mode = I915_TILING_NONE;
4693+ args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
4694+ }
4695+ }
4696+ obj_priv->tiling_mode = args->tiling_mode;
4697+
4698+ mutex_unlock(&dev->struct_mutex);
4699+
4700+ drm_gem_object_unreference(obj);
4701+
4702+ return 0;
4703+}
4704+
4705+/**
4706+ * Returns the current tiling mode and required bit 6 swizzling for the object.
4707+ */
4708+int
4709+i915_gem_get_tiling(struct drm_device *dev, void *data,
4710+ struct drm_file *file_priv)
4711+{
4712+ struct drm_i915_gem_get_tiling *args = data;
4713+ drm_i915_private_t *dev_priv = dev->dev_private;
4714+ struct drm_gem_object *obj;
4715+ struct drm_i915_gem_object *obj_priv;
4716+
4717+ obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4718+ if (obj == NULL)
4719+ return -EINVAL;
4720+ obj_priv = obj->driver_private;
4721+
4722+ mutex_lock(&dev->struct_mutex);
4723+
4724+ args->tiling_mode = obj_priv->tiling_mode;
4725+ switch (obj_priv->tiling_mode) {
4726+ case I915_TILING_X:
4727+ args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
4728+ break;
4729+ case I915_TILING_Y:
4730+ args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
4731+ break;
4732+ case I915_TILING_NONE:
4733+ args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
4734+ break;
4735+ default:
4736+ DRM_ERROR("unknown tiling mode\n");
4737+ }
4738+
4739+ mutex_unlock(&dev->struct_mutex);
4740+
4741+ drm_gem_object_unreference(obj);
4742+
4743+ return 0;
4744+}
4745--- a/drivers/gpu/drm/i915/i915_irq.c
4746+++ b/drivers/gpu/drm/i915/i915_irq.c
4747@@ -281,8 +281,10 @@ irqreturn_t i915_driver_irq_handler(DRM_
4748
4749 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
4750
4751- if (iir & I915_USER_INTERRUPT)
4752+ if (iir & I915_USER_INTERRUPT) {
4753+ dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
4754 DRM_WAKEUP(&dev_priv->irq_queue);
4755+ }
4756
4757 if (iir & (I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
4758 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT)) {
4759@@ -343,7 +345,7 @@ static int i915_emit_irq(struct drm_devi
4760 return dev_priv->counter;
4761 }
4762
4763-static void i915_user_irq_get(struct drm_device *dev)
4764+void i915_user_irq_get(struct drm_device *dev)
4765 {
4766 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4767
4768@@ -353,7 +355,7 @@ static void i915_user_irq_get(struct drm
4769 spin_unlock(&dev_priv->user_irq_lock);
4770 }
4771
4772-static void i915_user_irq_put(struct drm_device *dev)
4773+void i915_user_irq_put(struct drm_device *dev)
4774 {
4775 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4776
4777--- a/drivers/gpu/drm/i915/i915_reg.h
4778+++ b/drivers/gpu/drm/i915/i915_reg.h
4779@@ -25,19 +25,6 @@
4780 #ifndef _I915_REG_H_
4781 #define _I915_REG_H_
4782
4783-/* MCH MMIO space */
4784-/** 915-945 and GM965 MCH register controlling DRAM channel access */
4785-#define DCC 0x200
4786-#define DCC_ADDRESSING_MODE_SINGLE_CHANNEL (0 << 0)
4787-#define DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC (1 << 0)
4788-#define DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED (2 << 0)
4789-#define DCC_ADDRESSING_MODE_MASK (3 << 0)
4790-#define DCC_CHANNEL_XOR_DISABLE (1 << 10)
4791-
4792-/** 965 MCH register controlling DRAM channel configuration */
4793-#define CHDECMISC 0x111
4794-#define CHDECMISC_FLEXMEMORY (1 << 1)
4795-
4796 /*
4797 * The Bridge device's PCI config space has information about the
4798 * fb aperture size and the amount of pre-reserved memory.
4799@@ -516,6 +503,30 @@
4800 #define PALETTE_A 0x0a000
4801 #define PALETTE_B 0x0a800
4802
4803+/* MCH MMIO space */
4804+
4805+/*
4806+ * MCHBAR mirror.
4807+ *
4808+ * This mirrors the MCHBAR MMIO space whose location is determined by
4809+ * device 0 function 0's pci config register 0x44 or 0x48 and matches it in
4810+ * every way. It is not accessible from the CP register read instructions.
4811+ *
4812+ */
4813+/** 915-945 and GM965 MCH register controlling DRAM channel access */
4814+#define MCHBAR_MIRROR_BASE 0x10000
4815+
4816+#define DCC 0x10200
4817+#define DCC_ADDRESSING_MODE_SINGLE_CHANNEL (0 << 0)
4818+#define DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC (1 << 0)
4819+#define DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED (2 << 0)
4820+#define DCC_ADDRESSING_MODE_MASK (3 << 0)
4821+#define DCC_CHANNEL_XOR_DISABLE (1 << 10)
4822+
4823+/** 965 MCH register controlling DRAM channel configuration */
4824+#define CHDECMISC 0x10111
4825+#define CHDECMISC_FLEXMEMORY (1 << 1)
4826+
4827 /*
4828 * Overlay regs
4829 */
4830--- a/include/drm/drm.h
4831+++ b/include/drm/drm.h
4832@@ -573,6 +573,34 @@ struct drm_set_version {
4833 int drm_dd_minor;
4834 };
4835
4836+/** DRM_IOCTL_GEM_CLOSE ioctl argument type */
4837+struct drm_gem_close {
4838+ /** Handle of the object to be closed. */
4839+ uint32_t handle;
4840+ uint32_t pad;
4841+};
4842+
4843+/** DRM_IOCTL_GEM_FLINK ioctl argument type */
4844+struct drm_gem_flink {
4845+ /** Handle for the object being named */
4846+ uint32_t handle;
4847+
4848+ /** Returned global name */
4849+ uint32_t name;
4850+};
4851+
4852+/** DRM_IOCTL_GEM_OPEN ioctl argument type */
4853+struct drm_gem_open {
4854+ /** Name of object being opened */
4855+ uint32_t name;
4856+
4857+ /** Returned handle for the object */
4858+ uint32_t handle;
4859+
4860+ /** Returned size of the object */
4861+ uint64_t size;
4862+};
4863+
4864 #define DRM_IOCTL_BASE 'd'
4865 #define DRM_IO(nr) _IO(DRM_IOCTL_BASE,nr)
4866 #define DRM_IOR(nr,type) _IOR(DRM_IOCTL_BASE,nr,type)
4867@@ -587,6 +615,9 @@ struct drm_set_version {
4868 #define DRM_IOCTL_GET_CLIENT DRM_IOWR(0x05, struct drm_client)
4869 #define DRM_IOCTL_GET_STATS DRM_IOR( 0x06, struct drm_stats)
4870 #define DRM_IOCTL_SET_VERSION DRM_IOWR(0x07, struct drm_set_version)
4871+#define DRM_IOCTL_GEM_CLOSE DRM_IOW (0x09, struct drm_gem_close)
4872+#define DRM_IOCTL_GEM_FLINK DRM_IOWR(0x0a, struct drm_gem_flink)
4873+#define DRM_IOCTL_GEM_OPEN DRM_IOWR(0x0b, struct drm_gem_open)
4874
4875 #define DRM_IOCTL_SET_UNIQUE DRM_IOW( 0x10, struct drm_unique)
4876 #define DRM_IOCTL_AUTH_MAGIC DRM_IOW( 0x11, struct drm_auth)
4877--- a/include/drm/drmP.h
4878+++ b/include/drm/drmP.h
4879@@ -104,6 +104,7 @@ struct drm_device;
4880 #define DRIVER_DMA_QUEUE 0x200
4881 #define DRIVER_FB_DMA 0x400
4882 #define DRIVER_IRQ_VBL2 0x800
4883+#define DRIVER_GEM 0x1000
4884
4885 /***********************************************************************/
4886 /** \name Begin the DRM... */
4887@@ -387,6 +388,10 @@ struct drm_file {
4888 struct drm_minor *minor;
4889 int remove_auth_on_close;
4890 unsigned long lock_count;
4891+ /** Mapping of mm object handles to object pointers. */
4892+ struct idr object_idr;
4893+ /** Lock for synchronization of access to object_idr. */
4894+ spinlock_t table_lock;
4895 struct file *filp;
4896 void *driver_priv;
4897 };
4898@@ -558,6 +563,56 @@ struct drm_ati_pcigart_info {
4899 };
4900
4901 /**
4902+ * This structure defines the drm_mm memory object, which will be used by the
4903+ * DRM for its buffer objects.
4904+ */
4905+struct drm_gem_object {
4906+ /** Reference count of this object */
4907+ struct kref refcount;
4908+
4909+ /** Handle count of this object. Each handle also holds a reference */
4910+ struct kref handlecount;
4911+
4912+ /** Related drm device */
4913+ struct drm_device *dev;
4914+
4915+ /** File representing the shmem storage */
4916+ struct file *filp;
4917+
4918+ /**
4919+ * Size of the object, in bytes. Immutable over the object's
4920+ * lifetime.
4921+ */
4922+ size_t size;
4923+
4924+ /**
4925+ * Global name for this object, starts at 1. 0 means unnamed.
4926+ * Access is covered by the object_name_lock in the related drm_device
4927+ */
4928+ int name;
4929+
4930+ /**
4931+ * Memory domains. These monitor which caches contain read/write data
4932+ * related to the object. When transitioning from one set of domains
4933+ * to another, the driver is called to ensure that caches are suitably
4934+ * flushed and invalidated
4935+ */
4936+ uint32_t read_domains;
4937+ uint32_t write_domain;
4938+
4939+ /**
4940+ * While validating an exec operation, the
4941+ * new read/write domain values are computed here.
4942+ * They will be transferred to the above values
4943+ * at the point that any cache flushing occurs
4944+ */
4945+ uint32_t pending_read_domains;
4946+ uint32_t pending_write_domain;
4947+
4948+ void *driver_private;
4949+};
4950+
4951+/**
4952 * DRM driver structure. This structure represent the common code for
4953 * a family of cards. There will one drm_device for each card present
4954 * in this family
4955@@ -614,6 +669,18 @@ struct drm_driver {
4956 void (*set_version) (struct drm_device *dev,
4957 struct drm_set_version *sv);
4958
4959+ int (*proc_init)(struct drm_minor *minor);
4960+ void (*proc_cleanup)(struct drm_minor *minor);
4961+
4962+ /**
4963+ * Driver-specific constructor for drm_gem_objects, to set up
4964+ * obj->driver_private.
4965+ *
4966+ * Returns 0 on success.
4967+ */
4968+ int (*gem_init_object) (struct drm_gem_object *obj);
4969+ void (*gem_free_object) (struct drm_gem_object *obj);
4970+
4971 int major;
4972 int minor;
4973 int patchlevel;
4974@@ -771,6 +838,22 @@ struct drm_device {
4975 spinlock_t drw_lock;
4976 struct idr drw_idr;
4977 /*@} */
4978+
4979+ /** \name GEM information */
4980+ /*@{ */
4981+ spinlock_t object_name_lock;
4982+ struct idr object_name_idr;
4983+ atomic_t object_count;
4984+ atomic_t object_memory;
4985+ atomic_t pin_count;
4986+ atomic_t pin_memory;
4987+ atomic_t gtt_count;
4988+ atomic_t gtt_memory;
4989+ uint32_t gtt_total;
4990+ uint32_t invalidate_domains; /* domains pending invalidation */
4991+ uint32_t flush_domains; /* domains pending flush */
4992+ /*@} */
4993+
4994 };
4995
4996 static __inline__ int drm_core_check_feature(struct drm_device *dev,
4997@@ -867,6 +950,10 @@ extern void *drm_realloc(void *oldpt, si
4998 extern DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type);
4999 extern int drm_free_agp(DRM_AGP_MEM * handle, int pages);
5000 extern int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start);
5001+extern DRM_AGP_MEM *drm_agp_bind_pages(struct drm_device *dev,
5002+ struct page **pages,
5003+ unsigned long num_pages,
5004+ uint32_t gtt_offset);
5005 extern int drm_unbind_agp(DRM_AGP_MEM * handle);
5006
5007 /* Misc. IOCTL support (drm_ioctl.h) */
5008@@ -929,6 +1016,9 @@ extern int drm_getmagic(struct drm_devic
5009 extern int drm_authmagic(struct drm_device *dev, void *data,
5010 struct drm_file *file_priv);
5011
5012+/* Cache management (drm_cache.c) */
5013+void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
5014+
5015 /* Locking IOCTL support (drm_lock.h) */
5016 extern int drm_lock(struct drm_device *dev, void *data,
5017 struct drm_file *file_priv);
5018@@ -1026,6 +1116,7 @@ extern DRM_AGP_MEM *drm_agp_allocate_mem
5019 extern int drm_agp_free_memory(DRM_AGP_MEM * handle);
5020 extern int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start);
5021 extern int drm_agp_unbind_memory(DRM_AGP_MEM * handle);
5022+extern void drm_agp_chipset_flush(struct drm_device *dev);
5023
5024 /* Stub support (drm_stub.h) */
5025 extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
5026@@ -1088,6 +1179,66 @@ extern unsigned long drm_mm_tail_space(s
5027 extern int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size);
5028 extern int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size);
5029
5030+/* Graphics Execution Manager library functions (drm_gem.c) */
5031+int drm_gem_init(struct drm_device *dev);
5032+void drm_gem_object_free(struct kref *kref);
5033+struct drm_gem_object *drm_gem_object_alloc(struct drm_device *dev,
5034+ size_t size);
5035+void drm_gem_object_handle_free(struct kref *kref);
5036+
5037+static inline void
5038+drm_gem_object_reference(struct drm_gem_object *obj)
5039+{
5040+ kref_get(&obj->refcount);
5041+}
5042+
5043+static inline void
5044+drm_gem_object_unreference(struct drm_gem_object *obj)
5045+{
5046+ if (obj == NULL)
5047+ return;
5048+
5049+ kref_put(&obj->refcount, drm_gem_object_free);
5050+}
5051+
5052+int drm_gem_handle_create(struct drm_file *file_priv,
5053+ struct drm_gem_object *obj,
5054+ int *handlep);
5055+
5056+static inline void
5057+drm_gem_object_handle_reference(struct drm_gem_object *obj)
5058+{
5059+ drm_gem_object_reference(obj);
5060+ kref_get(&obj->handlecount);
5061+}
5062+
5063+static inline void
5064+drm_gem_object_handle_unreference(struct drm_gem_object *obj)
5065+{
5066+ if (obj == NULL)
5067+ return;
5068+
5069+ /*
5070+ * Must bump handle count first as this may be the last
5071+ * ref, in which case the object would disappear before we
5072+ * checked for a name
5073+ */
5074+ kref_put(&obj->handlecount, drm_gem_object_handle_free);
5075+ drm_gem_object_unreference(obj);
5076+}
5077+
5078+struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
5079+ struct drm_file *filp,
5080+ int handle);
5081+int drm_gem_close_ioctl(struct drm_device *dev, void *data,
5082+ struct drm_file *file_priv);
5083+int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
5084+ struct drm_file *file_priv);
5085+int drm_gem_open_ioctl(struct drm_device *dev, void *data,
5086+ struct drm_file *file_priv);
5087+void drm_gem_open(struct drm_device *dev, struct drm_file *file_private);
5088+void drm_gem_release(struct drm_device *dev, struct drm_file *file_private);
5089+
5090 extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev);
5091 extern void drm_core_ioremap_wc(struct drm_map *map, struct drm_device *dev);
5092 extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev);
5093--- a/include/drm/i915_drm.h
5094+++ b/include/drm/i915_drm.h
5095@@ -143,6 +143,22 @@ typedef struct _drm_i915_sarea {
5096 #define DRM_I915_GET_VBLANK_PIPE 0x0e
5097 #define DRM_I915_VBLANK_SWAP 0x0f
5098 #define DRM_I915_HWS_ADDR 0x11
5099+#define DRM_I915_GEM_INIT 0x13
5100+#define DRM_I915_GEM_EXECBUFFER 0x14
5101+#define DRM_I915_GEM_PIN 0x15
5102+#define DRM_I915_GEM_UNPIN 0x16
5103+#define DRM_I915_GEM_BUSY 0x17
5104+#define DRM_I915_GEM_THROTTLE 0x18
5105+#define DRM_I915_GEM_ENTERVT 0x19
5106+#define DRM_I915_GEM_LEAVEVT 0x1a
5107+#define DRM_I915_GEM_CREATE 0x1b
5108+#define DRM_I915_GEM_PREAD 0x1c
5109+#define DRM_I915_GEM_PWRITE 0x1d
5110+#define DRM_I915_GEM_MMAP 0x1e
5111+#define DRM_I915_GEM_SET_DOMAIN 0x1f
5112+#define DRM_I915_GEM_SW_FINISH 0x20
5113+#define DRM_I915_GEM_SET_TILING 0x21
5114+#define DRM_I915_GEM_GET_TILING 0x22
5115
5116 #define DRM_IOCTL_I915_INIT DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
5117 #define DRM_IOCTL_I915_FLUSH DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
5118@@ -160,6 +176,20 @@ typedef struct _drm_i915_sarea {
5119 #define DRM_IOCTL_I915_SET_VBLANK_PIPE DRM_IOW( DRM_COMMAND_BASE + DRM_I915_SET_VBLANK_PIPE, drm_i915_vblank_pipe_t)
5120 #define DRM_IOCTL_I915_GET_VBLANK_PIPE DRM_IOR( DRM_COMMAND_BASE + DRM_I915_GET_VBLANK_PIPE, drm_i915_vblank_pipe_t)
5121 #define DRM_IOCTL_I915_VBLANK_SWAP DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_VBLANK_SWAP, drm_i915_vblank_swap_t)
5122+#define DRM_IOCTL_I915_GEM_PIN DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_PIN, struct drm_i915_gem_pin)
5123+#define DRM_IOCTL_I915_GEM_UNPIN DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_UNPIN, struct drm_i915_gem_unpin)
5124+#define DRM_IOCTL_I915_GEM_BUSY DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_BUSY, struct drm_i915_gem_busy)
5125+#define DRM_IOCTL_I915_GEM_THROTTLE DRM_IO ( DRM_COMMAND_BASE + DRM_I915_GEM_THROTTLE)
5126+#define DRM_IOCTL_I915_GEM_ENTERVT DRM_IO(DRM_COMMAND_BASE + DRM_I915_GEM_ENTERVT)
5127+#define DRM_IOCTL_I915_GEM_LEAVEVT DRM_IO(DRM_COMMAND_BASE + DRM_I915_GEM_LEAVEVT)
5128+#define DRM_IOCTL_I915_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct drm_i915_gem_create)
5129+#define DRM_IOCTL_I915_GEM_PREAD DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_PREAD, struct drm_i915_gem_pread)
5130+#define DRM_IOCTL_I915_GEM_PWRITE DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_PWRITE, struct drm_i915_gem_pwrite)
5131+#define DRM_IOCTL_I915_GEM_MMAP DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MMAP, struct drm_i915_gem_mmap)
5132+#define DRM_IOCTL_I915_GEM_SET_DOMAIN DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_SET_DOMAIN, struct drm_i915_gem_set_domain)
5133+#define DRM_IOCTL_I915_GEM_SW_FINISH DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_SW_FINISH, struct drm_i915_gem_sw_finish)
5134+#define DRM_IOCTL_I915_GEM_SET_TILING DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_SET_TILING, struct drm_i915_gem_set_tiling)
5135+#define DRM_IOCTL_I915_GEM_GET_TILING DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_GET_TILING, struct drm_i915_gem_get_tiling)
5136
5137 /* Allow drivers to submit batchbuffers directly to hardware, relying
5138 * on the security mechanisms provided by hardware.
5139@@ -200,6 +230,7 @@ typedef struct drm_i915_irq_wait {
5140 #define I915_PARAM_IRQ_ACTIVE 1
5141 #define I915_PARAM_ALLOW_BATCHBUFFER 2
5142 #define I915_PARAM_LAST_DISPATCH 3
5143+#define I915_PARAM_HAS_GEM 5
5144
5145 typedef struct drm_i915_getparam {
5146 int param;
5147@@ -267,4 +298,305 @@ typedef struct drm_i915_hws_addr {
5148 uint64_t addr;
5149 } drm_i915_hws_addr_t;
5150
5151+struct drm_i915_gem_init {
5152+ /**
5153+ * Beginning offset in the GTT to be managed by the DRM memory
5154+ * manager.
5155+ */
5156+ uint64_t gtt_start;
5157+ /**
5158+ * Ending offset in the GTT to be managed by the DRM memory
5159+ * manager.
5160+ */
5161+ uint64_t gtt_end;
5162+};
5163+
5164+struct drm_i915_gem_create {
5165+ /**
5166+ * Requested size for the object.
5167+ *
5168+ * The (page-aligned) allocated size for the object will be returned.
5169+ */
5170+ uint64_t size;
5171+ /**
5172+ * Returned handle for the object.
5173+ *
5174+ * Object handles are nonzero.
5175+ */
5176+ uint32_t handle;
5177+ uint32_t pad;
5178+};
5179+
5180+struct drm_i915_gem_pread {
5181+ /** Handle for the object being read. */
5182+ uint32_t handle;
5183+ uint32_t pad;
5184+ /** Offset into the object to read from */
5185+ uint64_t offset;
5186+ /** Length of data to read */
5187+ uint64_t size;
5188+ /**
5189+ * Pointer to write the data into.
5190+ *
5191+ * This is a fixed-size type for 32/64 compatibility.
5192+ */
5193+ uint64_t data_ptr;
5194+};
5195+
5196+struct drm_i915_gem_pwrite {
5197+ /** Handle for the object being written to. */
5198+ uint32_t handle;
5199+ uint32_t pad;
5200+ /** Offset into the object to write to */
5201+ uint64_t offset;
5202+ /** Length of data to write */
5203+ uint64_t size;
5204+ /**
5205+ * Pointer to read the data from.
5206+ *
5207+ * This is a fixed-size type for 32/64 compatibility.
5208+ */
5209+ uint64_t data_ptr;
5210+};
5211+
5212+struct drm_i915_gem_mmap {
5213+ /** Handle for the object being mapped. */
5214+ uint32_t handle;
5215+ uint32_t pad;
5216+ /** Offset in the object to map. */
5217+ uint64_t offset;
5218+ /**
5219+ * Length of data to map.
5220+ *
5221+ * The value will be page-aligned.
5222+ */
5223+ uint64_t size;
5224+ /**
5225+ * Returned pointer the data was mapped at.
5226+ *
5227+ * This is a fixed-size type for 32/64 compatibility.
5228+ */
5229+ uint64_t addr_ptr;
5230+};
5231+
5232+struct drm_i915_gem_set_domain {
5233+ /** Handle for the object */
5234+ uint32_t handle;
5235+
5236+ /** New read domains */
5237+ uint32_t read_domains;
5238+
5239+ /** New write domain */
5240+ uint32_t write_domain;
5241+};
5242+
5243+struct drm_i915_gem_sw_finish {
5244+ /** Handle for the object */
5245+ uint32_t handle;
5246+};
5247+
5248+struct drm_i915_gem_relocation_entry {
5249+ /**
5250+ * Handle of the buffer being pointed to by this relocation entry.
5251+ *
5252+ * It's appealing to make this be an index into the mm_validate_entry
5253+ * list to refer to the buffer, but this allows the driver to create
5254+ * a relocation list for state buffers and not re-write it per
5255+ * exec using the buffer.
5256+ */
5257+ uint32_t target_handle;
5258+
5259+ /**
5260+ * Value to be added to the offset of the target buffer to make up
5261+ * the relocation entry.
5262+ */
5263+ uint32_t delta;
5264+
5265+ /** Offset in the buffer the relocation entry will be written into */
5266+ uint64_t offset;
5267+
5268+ /**
5269+ * Offset value of the target buffer that the relocation entry was last
5270+ * written as.
5271+ *
5272+ * If the buffer has the same offset as last time, we can skip syncing
5273+ * and writing the relocation. This value is written back out by
5274+ * the execbuffer ioctl when the relocation is written.
5275+ */
5276+ uint64_t presumed_offset;
5277+
5278+ /**
5279+ * Target memory domains read by this operation.
5280+ */
5281+ uint32_t read_domains;
5282+
5283+ /**
5284+ * Target memory domains written by this operation.
5285+ *
5286+ * Note that only one domain may be written by the whole
5287+ * execbuffer operation, so that where there are conflicts,
5288+ * the application will get -EINVAL back.
5289+ */
5290+ uint32_t write_domain;
5291+};
5292+
5293+/** @{
5294+ * Intel memory domains
5295+ *
5296+ * Most of these just align with the various caches in
5297+ * the system and are used to flush and invalidate as
5298+ * objects end up cached in different domains.
5299+ */
5300+/** CPU cache */
5301+#define I915_GEM_DOMAIN_CPU 0x00000001
5302+/** Render cache, used by 2D and 3D drawing */
5303+#define I915_GEM_DOMAIN_RENDER 0x00000002
5304+/** Sampler cache, used by texture engine */
5305+#define I915_GEM_DOMAIN_SAMPLER 0x00000004
5306+/** Command queue, used to load batch buffers */
5307+#define I915_GEM_DOMAIN_COMMAND 0x00000008
5308+/** Instruction cache, used by shader programs */
5309+#define I915_GEM_DOMAIN_INSTRUCTION 0x00000010
5310+/** Vertex address cache */
5311+#define I915_GEM_DOMAIN_VERTEX 0x00000020
5312+/** GTT domain - aperture and scanout */
5313+#define I915_GEM_DOMAIN_GTT 0x00000040
5314+/** @} */
5315+
5316+struct drm_i915_gem_exec_object {
5317+ /**
5318+ * User's handle for a buffer to be bound into the GTT for this
5319+ * operation.
5320+ */
5321+ uint32_t handle;
5322+
5323+ /** Number of relocations to be performed on this buffer */
5324+ uint32_t relocation_count;
5325+ /**
5326+ * Pointer to array of struct drm_i915_gem_relocation_entry containing
5327+ * the relocations to be performed in this buffer.
5328+ */
5329+ uint64_t relocs_ptr;
5330+
5331+ /** Required alignment in graphics aperture */
5332+ uint64_t alignment;
5333+
5334+ /**
5335+ * Returned value of the updated offset of the object, for future
5336+ * presumed_offset writes.
5337+ */
5338+ uint64_t offset;
5339+};
5340+
5341+struct drm_i915_gem_execbuffer {
5342+ /**
5343+ * List of buffers to be validated with their relocations to be
5344+ * performend on them.
5345+ *
5346+ * This is a pointer to an array of struct drm_i915_gem_validate_entry.
5347+ *
5348+ * These buffers must be listed in an order such that all relocations
5349+ * a buffer is performing refer to buffers that have already appeared
5350+ * in the validate list.
5351+ */
5352+ uint64_t buffers_ptr;
5353+ uint32_t buffer_count;
5354+
5355+ /** Offset in the batchbuffer to start execution from. */
5356+ uint32_t batch_start_offset;
5357+ /** Bytes used in batchbuffer from batch_start_offset */
5358+ uint32_t batch_len;
5359+ uint32_t DR1;
5360+ uint32_t DR4;
5361+ uint32_t num_cliprects;
5362+ /** This is a struct drm_clip_rect *cliprects */
5363+ uint64_t cliprects_ptr;
5364+};
5365+
5366+struct drm_i915_gem_pin {
5367+ /** Handle of the buffer to be pinned. */
5368+ uint32_t handle;
5369+ uint32_t pad;
5370+
5371+ /** alignment required within the aperture */
5372+ uint64_t alignment;
5373+
5374+ /** Returned GTT offset of the buffer. */
5375+ uint64_t offset;
5376+};
5377+
5378+struct drm_i915_gem_unpin {
5379+ /** Handle of the buffer to be unpinned. */
5380+ uint32_t handle;
5381+ uint32_t pad;
5382+};
5383+
5384+struct drm_i915_gem_busy {
5385+ /** Handle of the buffer to check for busy */
5386+ uint32_t handle;
5387+
5388+ /** Return busy status (1 if busy, 0 if idle) */
5389+ uint32_t busy;
5390+};
5391+
5392+#define I915_TILING_NONE 0
5393+#define I915_TILING_X 1
5394+#define I915_TILING_Y 2
5395+
5396+#define I915_BIT_6_SWIZZLE_NONE 0
5397+#define I915_BIT_6_SWIZZLE_9 1
5398+#define I915_BIT_6_SWIZZLE_9_10 2
5399+#define I915_BIT_6_SWIZZLE_9_11 3
5400+#define I915_BIT_6_SWIZZLE_9_10_11 4
5401+/* Not seen by userland */
5402+#define I915_BIT_6_SWIZZLE_UNKNOWN 5
5403+
5404+struct drm_i915_gem_set_tiling {
5405+ /** Handle of the buffer to have its tiling state updated */
5406+ uint32_t handle;
5407+
5408+ /**
5409+ * Tiling mode for the object (I915_TILING_NONE, I915_TILING_X,
5410+ * I915_TILING_Y).
5411+ *
5412+ * This value is to be set on request, and will be updated by the
5413+ * kernel on successful return with the actual chosen tiling layout.
5414+ *
5415+ * The tiling mode may be demoted to I915_TILING_NONE when the system
5416+ * has bit 6 swizzling that can't be managed correctly by GEM.
5417+ *
5418+ * Buffer contents become undefined when changing tiling_mode.
5419+ */
5420+ uint32_t tiling_mode;
5421+
5422+ /**
5423+ * Stride in bytes for the object when in I915_TILING_X or
5424+ * I915_TILING_Y.
5425+ */
5426+ uint32_t stride;
5427+
5428+ /**
5429+ * Returned address bit 6 swizzling required for CPU access through
5430+ * mmap mapping.
5431+ */
5432+ uint32_t swizzle_mode;
5433+};
5434+
5435+struct drm_i915_gem_get_tiling {
5436+ /** Handle of the buffer to get tiling state for. */
5437+ uint32_t handle;
5438+
5439+ /**
5440+ * Current tiling mode for the object (I915_TILING_NONE, I915_TILING_X,
5441+ * I915_TILING_Y).
5442+ */
5443+ uint32_t tiling_mode;
5444+
5445+ /**
5446+ * Returned address bit 6 swizzling required for CPU access through
5447+ * mmap mapping.
5448+ */
5449+ uint32_t swizzle_mode;
5450+};
5451+
5452 #endif /* _I915_DRM_H_ */
5453