From c82894034f611696c54c5aaf2112be638aa2cb35 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 4 Mar 2008 15:08:24 -0800 Subject: Clarify that drm_agp_ttm_backend is associated with a drm_ttm. On first looking at it I assumed it was an aspect of the ttm backend as a whole rather than specific allocations from a backend. --- linux-core/drm_objects.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/drm_objects.h b/linux-core/drm_objects.h index 770fbc56..6a900612 100644 --- a/linux-core/drm_objects.h +++ b/linux-core/drm_objects.h @@ -301,7 +301,12 @@ struct drm_ttm_backend_func { void (*destroy) (struct drm_ttm_backend *backend); }; - +/** + * This structure associates a set of flags and methods with a drm_ttm + * object, and will also be subclassed by the particular backend. + * + * \sa #drm_agp_ttm_backend + */ struct drm_ttm_backend { struct drm_device *dev; uint32_t flags; -- cgit v1.2.3 From 47a2b7dc03e35d4eaf8148b87aeea8dd96723b4d Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 22 Apr 2008 16:08:23 -0700 Subject: Initial add of mmfs module. --- linux-core/Makefile | 3 +- linux-core/Makefile.kernel | 3 +- linux-core/mmfs.h | 176 ++++++++++++++++++++++++++ linux-core/mmfs_drv.c | 299 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 479 insertions(+), 2 deletions(-) create mode 100644 linux-core/mmfs.h create mode 100644 linux-core/mmfs_drv.c (limited to 'linux-core') diff --git a/linux-core/Makefile b/linux-core/Makefile index 3af6f370..f2d44642 100644 --- a/linux-core/Makefile +++ b/linux-core/Makefile @@ -58,7 +58,7 @@ endif # Modules for all architectures MODULE_LIST := drm.o tdfx.o r128.o radeon.o mga.o sis.o savage.o via.o \ - mach64.o nv.o nouveau.o xgi.o + mach64.o nv.o nouveau.o xgi.o mmfs.o # Modules only for ix86 architectures ifneq (,$(findstring 86,$(MACHINE))) @@ -92,6 +92,7 @@ NVHEADERS = nv_drv.h $(DRMHEADERS) FFBHEADERS = ffb_drv.h $(DRMHEADERS) NOUVEAUHEADERS = nouveau_drv.h nouveau_drm.h nouveau_reg.h $(DRMHEADERS) XGIHEADERS = xgi_cmdlist.h xgi_drv.h xgi_misc.h xgi_regs.h $(DRMHEADERS) +MMFSHEADERS = mmfs.h mmfs_priv.h PROGS = dristat drmstat diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index f012262d..093552bf 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -41,6 +41,7 @@ mach64-objs := mach64_drv.o mach64_dma.o mach64_irq.o mach64_state.o nv-objs := nv_drv.o xgi-objs := xgi_cmdlist.o xgi_drv.o xgi_fb.o xgi_misc.o xgi_pcie.o \ xgi_fence.o +mmfs-objs := mmfs_drv.o ifeq ($(CONFIG_COMPAT),y) drm-objs += drm_ioc32.o @@ -52,7 +53,7 @@ nouveau-objs += nouveau_ioc32.o xgi-objs += xgi_ioc32.o endif -obj-m += drm.o +obj-m += drm.o mmfs.o obj-$(CONFIG_DRM_TDFX) += tdfx.o obj-$(CONFIG_DRM_R128) += r128.o obj-$(CONFIG_DRM_RADEON)+= radeon.o diff --git a/linux-core/mmfs.h b/linux-core/mmfs.h new file mode 100644 index 00000000..bdc148b9 --- /dev/null +++ b/linux-core/mmfs.h @@ -0,0 +1,176 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +/** @file mmfs.h + * This file provides ioctl and ioctl argument definitions for using the + * mmfs device. + */ +#ifdef __KERNEL__ +#include +#include + +/** @file mmfs_priv.h + * This file provides internal structure definitions for mmfs.. + */ + +/** + * This structure defines the mmfs memory object, which will be used by the + * DRM for its buffer objects. + */ +struct mmfs_object { + /** File representing the shmem storage */ + struct file *filp; + + spinlock_t lock; + + size_t size; + /** Reference count of this object, protected by object_lock */ + int refcount; +}; + +/** + * This structure defines the process (actually per-fd) mapping of object + * handles to mmfs objects. + */ +struct mmfs_file { + /** Mapping of object handles to object pointers. */ + struct idr object_idr; + /** + * Lock for synchronization of access to object->refcount and + * object_idr. See note in mmfs_unreference_ioctl. + */ + spinlock_t delete_lock; +}; + +void mmfs_object_reference(struct mmfs_object *obj); +void mmfs_object_unreference(struct mmfs_object *obj); + +#endif /* __KERNEL__ */ + +#define MMFS_DEVICE_PATH "/dev/mmfs" +/* XXX: Choose non-experimental major */ +#define MMFS_DEVICE_MAJOR 246 + +struct mmfs_alloc_args { + /** + * Requested size for the object. + * + * The (page-aligned) allocated size for the object will be returned. + */ + uint32_t size; + /** Returned handle for the object. */ + uint32_t handle; +}; + +struct mmfs_unreference_args { + /** Handle of the object to be unreferenced. */ + uint32_t handle; +}; + +struct mmfs_link_args { + /** Handle for the object being given a name. */ + uint32_t handle; + /** Requested file name to export the object under. */ + char *name; + /** Requested file mode to export the object under. */ + mode_t mode; +}; + +struct mmfs_pread_args { + /** Handle for the object being read. */ + uint32_t handle; + /** Offset into the object to read from */ + off_t offset; + /** Length of data to read */ + size_t size; + /** Pointer to write the data into. */ + void *data; +}; + +struct mmfs_pwrite_args { + /** Handle for the object being written to. */ + uint32_t handle; + /** Offset into the object to write to */ + off_t offset; + /** Length of data to write */ + size_t size; + /** Pointer to read the data from. */ + void *data; +}; + +struct mmfs_mmap_args { + /** Handle for the object being mapped. */ + uint32_t handle; + /** Offset in the object to map. */ + off_t offset; + /** + * Length of data to map. + * + * The value will be page-aligned. + */ + size_t size; + /** Returned pointer the data was mapped at */ + void *addr; +}; + +/** + * \name Ioctls Definitions + */ +/* @{ */ + +#define MMFS_IOCTL_BASE 'm' +#define MMFS_IO(nr) _IO(MMFS_IOCTL_BASE, nr) +#define MMFS_IOR(nr,type) _IOR(MMFS_IOCTL_BASE, nr, type) +#define MMFS_IOW(nr,type) _IOW(MMFS_IOCTL_BASE, nr, type) +#define MMFS_IOWR(nr,type) _IOWR(MMFS_IOCTL_BASE, nr, type) + +/** This ioctl allocates an object and returns a handle referencing it. */ +#define MMFS_IOCTL_ALLOC MMFS_IOWR(0x00, struct mmfs_alloc_args) + +/** + * This ioctl releases the reference on the handle returned from + * MMFS_IOCTL_ALLOC. + */ +#define MMFS_IOCTL_UNREFERENCE MMFS_IOR(0x01, struct mmfs_unreference_args) + +/** + * This ioctl creates a file in the mmfs filesystem representing an object. + * + * XXX: Need a way to get handle from fd or name. + */ +#define MMFS_IOCTL_LINK MMFS_IOWR(0x02, struct mmfs_link_args) + +/** This ioctl copies data from an object into a user address. */ +#define MMFS_IOCTL_PREAD MMFS_IOWR(0x03, struct mmfs_pread_args) + +/** This ioctl copies data from a user address into an object. */ +#define MMFS_IOCTL_PWRITE MMFS_IOWR(0x04, struct mmfs_pwrite_args) + +/** This ioctl maps data from the object into the user address space. */ +#define MMFS_IOCTL_MMAP MMFS_IOWR(0x05, struct mmfs_mmap_args) + +/* }@ */ diff --git a/linux-core/mmfs_drv.c b/linux-core/mmfs_drv.c new file mode 100644 index 00000000..f4b07117 --- /dev/null +++ b/linux-core/mmfs_drv.c @@ -0,0 +1,299 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "mmfs.h" + +/** @file mmfs.c + * + * This file provides the filesystem for memory manager objects used by the + * DRM. + * + * The goal is to have swap-backed object allocation managed through + * struct file. However, file descriptors as handles to a struct file have + * two major failings: + * - Process limits prevent more than 1024 or so being used at a time by + * default. + * - Inability to allocate high fds will aggravate the X Server's select() + * handling, and likely that of many GL client applications as well. + * + * This led to a plan of using our own integer IDs (called handles, following + * DRM terminology) to mimic fds, and implement the fd syscalls we need as + * ioctls. The objects themselves will still include the struct file so + * that we can transition to fds if the required kernel infrastructure shows + * up at a later data, and as our interface with shmfs for memory allocation. + */ + +static struct mmfs_object * +mmfs_object_alloc(size_t size) +{ + struct mmfs_object *obj; + + BUG_ON((size & (PAGE_SIZE - 1)) != 0); + + obj = kcalloc(1, sizeof(*obj), GFP_KERNEL); + + obj->filp = shmem_file_setup("mmfs object", size, 0); + if (IS_ERR(obj->filp)) { + kfree(obj); + return NULL; + } + + obj->refcount = 1; + + return obj; +} + +/** + * Removes the mapping from handle to filp for this object. + */ +static int +mmfs_handle_delete(struct mmfs_file *mmfs_filp, int handle) +{ + struct mmfs_object *obj; + + /* This is gross. The idr system doesn't let us try a delete and + * return an error code. It just spews if you fail at deleting. + * So, we have to grab a lock around finding the object and then + * doing the delete on it and dropping the refcount, or the user + * could race us to double-decrement the refcount and cause a + * use-after-free later. Given the frequency of our handle lookups, + * we may want to use ida for number allocation and a hash table + * for the pointers, anyway. + */ + spin_lock(&mmfs_filp->delete_lock); + + /* Check if we currently have a reference on the object */ + obj = idr_find(&mmfs_filp->object_idr, handle); + if (obj == NULL) { + spin_unlock(&mmfs_filp->delete_lock); + return -EINVAL; + } + + /* Release reference and decrement refcount. */ + idr_remove(&mmfs_filp->object_idr, handle); + mmfs_object_unreference(obj); + + spin_unlock(&mmfs_filp->delete_lock); + + return 0; +} + +/** + * Allocates a new mmfs object and returns a handle to it. + */ +static int +mmfs_alloc_ioctl(struct inode *inode, struct file *filp, + unsigned int cmd, unsigned long arg) +{ + struct mmfs_file *mmfs_filp = filp->private_data; + struct mmfs_alloc_args args; + struct mmfs_object *obj; + int handle, ret; + + if (copy_from_user(&args, (void __user *)arg, sizeof(args))) + return -EFAULT; + + /* Round requested size up to page size */ + args.size = (args.size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + + /* Allocate the new object */ + obj = mmfs_object_alloc(args.size); + if (obj == NULL) + return -ENOMEM; + + /* Get the user-visible handle using idr. + * + * I'm not really sure why the idr api needs us to do this in two + * repeating steps. It handles internal locking of its data + * structure, yet insists that we keep its memory allocation step + * separate from its slot-finding step for locking purposes. + */ + do { + if (idr_pre_get(&mmfs_filp->object_idr, GFP_KERNEL) == 0) { + kfree(obj); + return -EFAULT; + } + + ret = idr_get_new(&mmfs_filp->object_idr, obj, &handle); + } while (ret == -EAGAIN); + + if (ret != 0) { + mmfs_object_unreference(obj); + return -EFAULT; + } + + args.handle = handle; + + if (copy_to_user((void __user *)arg, &args, sizeof(args))) { + mmfs_handle_delete(mmfs_filp, args.handle); + return -EFAULT; + } + + return 0; +} + +/** + * Allocates a new mmfs object and returns a handle to it. + */ +static int +mmfs_unreference_ioctl(struct inode *inode, struct file *filp, + unsigned int cmd, unsigned long arg) +{ + struct mmfs_file *mmfs_filp = filp->private_data; + struct mmfs_unreference_args args; + int ret; + + if (copy_from_user(&args, (void __user *)arg, sizeof(args))) + return -EFAULT; + + ret = mmfs_handle_delete(mmfs_filp, args.handle); + + return ret; +} + +static int +mmfs_ioctl(struct inode *inode, struct file *filp, + unsigned int cmd, unsigned long arg) +{ + + switch (cmd) { + case MMFS_IOCTL_ALLOC: + return mmfs_alloc_ioctl(inode, filp, cmd, arg); + case MMFS_IOCTL_UNREFERENCE: + return mmfs_unreference_ioctl(inode, filp, cmd, arg); + default: + return -EINVAL; + } +} + +/** + * Sets up the file private for keeping track of our mappings of handles to + * mmfs objects. + */ +int +mmfs_open(struct inode *inode, struct file *filp) +{ + struct mmfs_file *mmfs_filp; + + if (filp->f_flags & O_EXCL) + return -EBUSY; /* No exclusive opens */ + + mmfs_filp = kcalloc(1, sizeof(*mmfs_filp), GFP_KERNEL); + if (mmfs_filp == NULL) + return -ENOMEM; + filp->private_data = mmfs_filp; + + idr_init(&mmfs_filp->object_idr); + + return 0; +} + +/** Called at device close to release the file's references on objects. */ +static int +mmfs_object_release(int id, void *ptr, void *data) +{ + struct mmfs_object *obj = ptr; + + mmfs_object_unreference(obj); + + return 0; +} + +/** + * Called at close time when the filp is going away. + * + * Releases any remaining references on objects by this filp. + */ +int +mmfs_close(struct inode *inode, struct file *filp) +{ + struct mmfs_file *mmfs_filp = filp->private_data; + + idr_for_each(&mmfs_filp->object_idr, &mmfs_object_release, NULL); + + idr_destroy(&mmfs_filp->object_idr); + + kfree(mmfs_filp); + filp->private_data = NULL; + + return 0; +} + +void +mmfs_object_reference(struct mmfs_object *obj) +{ + spin_lock(&obj->lock); + obj->refcount++; + spin_unlock(&obj->lock); +} + +void +mmfs_object_unreference(struct mmfs_object *obj) +{ + spin_lock(&obj->lock); + obj->refcount--; + spin_unlock(&obj->lock); + if (obj->refcount == 0) { + fput(obj->filp); + kfree(obj); + } +} + +/** File operations structure */ +static const struct file_operations mmfs_dev_fops = { + .owner = THIS_MODULE, + .open = mmfs_open, + .release = mmfs_close, + .ioctl = mmfs_ioctl, +}; + +static int __init mmfs_init(void) +{ + int ret; + + ret = register_chrdev(MMFS_DEVICE_MAJOR, "mmfs", &mmfs_dev_fops); + if (ret != 0) + return ret; + + return 0; +} + +static void __exit mmfs_exit(void) +{ + unregister_chrdev(MMFS_DEVICE_MAJOR, "mmfs"); +} + +module_init(mmfs_init); +module_exit(mmfs_exit); +MODULE_LICENSE("GPL and additional rights"); -- cgit v1.2.3 From 8665b666c7e2ecdee7d27e1ad540910a0223ba6d Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 23 Apr 2008 11:22:59 -0700 Subject: Move mmfs.h userland interface to shared-core. --- linux-core/mmfs.h | 177 +------------------------------------------------- linux-core/mmfs_drv.h | 65 ++++++++++++++++++ 2 files changed, 66 insertions(+), 176 deletions(-) mode change 100644 => 120000 linux-core/mmfs.h create mode 100644 linux-core/mmfs_drv.h (limited to 'linux-core') diff --git a/linux-core/mmfs.h b/linux-core/mmfs.h deleted file mode 100644 index bdc148b9..00000000 --- a/linux-core/mmfs.h +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright © 2008 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - * - * Authors: - * Eric Anholt - * - */ - -/** @file mmfs.h - * This file provides ioctl and ioctl argument definitions for using the - * mmfs device. - */ -#ifdef __KERNEL__ -#include -#include - -/** @file mmfs_priv.h - * This file provides internal structure definitions for mmfs.. - */ - -/** - * This structure defines the mmfs memory object, which will be used by the - * DRM for its buffer objects. - */ -struct mmfs_object { - /** File representing the shmem storage */ - struct file *filp; - - spinlock_t lock; - - size_t size; - /** Reference count of this object, protected by object_lock */ - int refcount; -}; - -/** - * This structure defines the process (actually per-fd) mapping of object - * handles to mmfs objects. - */ -struct mmfs_file { - /** Mapping of object handles to object pointers. */ - struct idr object_idr; - /** - * Lock for synchronization of access to object->refcount and - * object_idr. See note in mmfs_unreference_ioctl. - */ - spinlock_t delete_lock; -}; - -void mmfs_object_reference(struct mmfs_object *obj); -void mmfs_object_unreference(struct mmfs_object *obj); - -#endif /* __KERNEL__ */ - -#define MMFS_DEVICE_PATH "/dev/mmfs" -/* XXX: Choose non-experimental major */ -#define MMFS_DEVICE_MAJOR 246 - -struct mmfs_alloc_args { - /** - * Requested size for the object. - * - * The (page-aligned) allocated size for the object will be returned. - */ - uint32_t size; - /** Returned handle for the object. */ - uint32_t handle; -}; - -struct mmfs_unreference_args { - /** Handle of the object to be unreferenced. */ - uint32_t handle; -}; - -struct mmfs_link_args { - /** Handle for the object being given a name. */ - uint32_t handle; - /** Requested file name to export the object under. */ - char *name; - /** Requested file mode to export the object under. */ - mode_t mode; -}; - -struct mmfs_pread_args { - /** Handle for the object being read. */ - uint32_t handle; - /** Offset into the object to read from */ - off_t offset; - /** Length of data to read */ - size_t size; - /** Pointer to write the data into. */ - void *data; -}; - -struct mmfs_pwrite_args { - /** Handle for the object being written to. */ - uint32_t handle; - /** Offset into the object to write to */ - off_t offset; - /** Length of data to write */ - size_t size; - /** Pointer to read the data from. */ - void *data; -}; - -struct mmfs_mmap_args { - /** Handle for the object being mapped. */ - uint32_t handle; - /** Offset in the object to map. */ - off_t offset; - /** - * Length of data to map. - * - * The value will be page-aligned. - */ - size_t size; - /** Returned pointer the data was mapped at */ - void *addr; -}; - -/** - * \name Ioctls Definitions - */ -/* @{ */ - -#define MMFS_IOCTL_BASE 'm' -#define MMFS_IO(nr) _IO(MMFS_IOCTL_BASE, nr) -#define MMFS_IOR(nr,type) _IOR(MMFS_IOCTL_BASE, nr, type) -#define MMFS_IOW(nr,type) _IOW(MMFS_IOCTL_BASE, nr, type) -#define MMFS_IOWR(nr,type) _IOWR(MMFS_IOCTL_BASE, nr, type) - -/** This ioctl allocates an object and returns a handle referencing it. */ -#define MMFS_IOCTL_ALLOC MMFS_IOWR(0x00, struct mmfs_alloc_args) - -/** - * This ioctl releases the reference on the handle returned from - * MMFS_IOCTL_ALLOC. - */ -#define MMFS_IOCTL_UNREFERENCE MMFS_IOR(0x01, struct mmfs_unreference_args) - -/** - * This ioctl creates a file in the mmfs filesystem representing an object. - * - * XXX: Need a way to get handle from fd or name. - */ -#define MMFS_IOCTL_LINK MMFS_IOWR(0x02, struct mmfs_link_args) - -/** This ioctl copies data from an object into a user address. */ -#define MMFS_IOCTL_PREAD MMFS_IOWR(0x03, struct mmfs_pread_args) - -/** This ioctl copies data from a user address into an object. */ -#define MMFS_IOCTL_PWRITE MMFS_IOWR(0x04, struct mmfs_pwrite_args) - -/** This ioctl maps data from the object into the user address space. */ -#define MMFS_IOCTL_MMAP MMFS_IOWR(0x05, struct mmfs_mmap_args) - -/* }@ */ diff --git a/linux-core/mmfs.h b/linux-core/mmfs.h new file mode 120000 index 00000000..3e589504 --- /dev/null +++ b/linux-core/mmfs.h @@ -0,0 +1 @@ +../shared-core/mmfs.h \ No newline at end of file diff --git a/linux-core/mmfs_drv.h b/linux-core/mmfs_drv.h new file mode 100644 index 00000000..1944d2af --- /dev/null +++ b/linux-core/mmfs_drv.h @@ -0,0 +1,65 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +#include +#include + +/** @file mmfs_drv.h + * This file provides structure definitions and function prototypes for mmfs. + */ + +/** + * This structure defines the mmfs memory object, which will be used by the + * DRM for its buffer objects. + */ +struct mmfs_object { + /** File representing the shmem storage */ + struct file *filp; + + spinlock_t lock; + + size_t size; + /** Reference count of this object, protected by object_lock */ + int refcount; +}; + +/** + * This structure defines the process (actually per-fd) mapping of object + * handles to mmfs objects. + */ +struct mmfs_file { + /** Mapping of object handles to object pointers. */ + struct idr object_idr; + /** + * Lock for synchronization of access to object->refcount and + * object_idr. See note in mmfs_unreference_ioctl. + */ + spinlock_t delete_lock; +}; + +void mmfs_object_reference(struct mmfs_object *obj); +void mmfs_object_unreference(struct mmfs_object *obj); -- cgit v1.2.3 From 8c741ed54e1be63528e79222b600f37506c6d6d2 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 23 Apr 2008 13:06:58 -0700 Subject: Add pread/pwrite ioctls to mmfs. --- linux-core/mmfs_drv.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++-- linux-core/mmfs_drv.h | 7 +--- 2 files changed, 111 insertions(+), 9 deletions(-) (limited to 'linux-core') diff --git a/linux-core/mmfs_drv.c b/linux-core/mmfs_drv.c index f4b07117..d973592d 100644 --- a/linux-core/mmfs_drv.c +++ b/linux-core/mmfs_drv.c @@ -91,12 +91,12 @@ mmfs_handle_delete(struct mmfs_file *mmfs_filp, int handle) * we may want to use ida for number allocation and a hash table * for the pointers, anyway. */ - spin_lock(&mmfs_filp->delete_lock); + spin_lock(&mmfs_filp->table_lock); /* Check if we currently have a reference on the object */ obj = idr_find(&mmfs_filp->object_idr, handle); if (obj == NULL) { - spin_unlock(&mmfs_filp->delete_lock); + spin_unlock(&mmfs_filp->table_lock); return -EINVAL; } @@ -104,11 +104,34 @@ mmfs_handle_delete(struct mmfs_file *mmfs_filp, int handle) idr_remove(&mmfs_filp->object_idr, handle); mmfs_object_unreference(obj); - spin_unlock(&mmfs_filp->delete_lock); + spin_unlock(&mmfs_filp->table_lock); return 0; } +/** Returns a reference to the object named by the handle. */ +static struct mmfs_object * +mmfs_object_lookup(struct mmfs_file *mmfs_filp, int handle) +{ + struct mmfs_object *obj; + + spin_lock(&mmfs_filp->table_lock); + + /* Check if we currently have a reference on the object */ + obj = idr_find(&mmfs_filp->object_idr, handle); + if (obj == NULL) { + spin_unlock(&mmfs_filp->table_lock); + return NULL; + } + + mmfs_object_reference(obj); + + spin_unlock(&mmfs_filp->table_lock); + + return obj; +} + + /** * Allocates a new mmfs object and returns a handle to it. */ @@ -164,7 +187,7 @@ mmfs_alloc_ioctl(struct inode *inode, struct file *filp, } /** - * Allocates a new mmfs object and returns a handle to it. + * Releases the handle to an mmfs object. */ static int mmfs_unreference_ioctl(struct inode *inode, struct file *filp, @@ -182,6 +205,84 @@ mmfs_unreference_ioctl(struct inode *inode, struct file *filp, return ret; } +/** + * Reads data from the object referenced by handle. + * + * On error, the contents of *data are undefined. + */ +static int +mmfs_pread_ioctl(struct inode *inode, struct file *filp, + unsigned int cmd, unsigned long arg) +{ + struct mmfs_file *mmfs_filp = filp->private_data; + struct mmfs_pread_args args; + struct mmfs_object *obj; + ssize_t read; + loff_t offset; + + if (copy_from_user(&args, (void __user *)arg, sizeof(args))) + return -EFAULT; + + obj = mmfs_object_lookup(mmfs_filp, args.handle); + if (obj == NULL) + return -EINVAL; + + offset = args.offset; + + read = obj->filp->f_op->read(obj->filp, (char __user *)args.data, + args.size, &offset); + if (read != args.size) { + mmfs_object_unreference(obj); + if (read < 0) + return read; + else + return -EINVAL; + } + + mmfs_object_unreference(obj); + + return 0; +} + +/** + * Writes data to the object referenced by handle. + * + * On error, the contents of the buffer that were to be modified are undefined. + */ +static int +mmfs_pwrite_ioctl(struct inode *inode, struct file *filp, + unsigned int cmd, unsigned long arg) +{ + struct mmfs_file *mmfs_filp = filp->private_data; + struct mmfs_pwrite_args args; + struct mmfs_object *obj; + ssize_t written; + loff_t offset; + + if (copy_from_user(&args, (void __user *)arg, sizeof(args))) + return -EFAULT; + + obj = mmfs_object_lookup(mmfs_filp, args.handle); + if (obj == NULL) + return -EINVAL; + + offset = args.offset; + + written = obj->filp->f_op->write(obj->filp, (char __user *)args.data, + args.size, &offset); + if (written != args.size) { + mmfs_object_unreference(obj); + if (written < 0) + return written; + else + return -EINVAL; + } + + mmfs_object_unreference(obj); + + return 0; +} + static int mmfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) @@ -192,6 +293,10 @@ mmfs_ioctl(struct inode *inode, struct file *filp, return mmfs_alloc_ioctl(inode, filp, cmd, arg); case MMFS_IOCTL_UNREFERENCE: return mmfs_unreference_ioctl(inode, filp, cmd, arg); + case MMFS_IOCTL_PREAD: + return mmfs_pread_ioctl(inode, filp, cmd, arg); + case MMFS_IOCTL_PWRITE: + return mmfs_pwrite_ioctl(inode, filp, cmd, arg); default: return -EINVAL; } diff --git a/linux-core/mmfs_drv.h b/linux-core/mmfs_drv.h index 1944d2af..53d2f6cb 100644 --- a/linux-core/mmfs_drv.h +++ b/linux-core/mmfs_drv.h @@ -54,11 +54,8 @@ struct mmfs_object { struct mmfs_file { /** Mapping of object handles to object pointers. */ struct idr object_idr; - /** - * Lock for synchronization of access to object->refcount and - * object_idr. See note in mmfs_unreference_ioctl. - */ - spinlock_t delete_lock; + /** Lock for synchronization of access to object_idr. */ + spinlock_t table_lock; }; void mmfs_object_reference(struct mmfs_object *obj); -- cgit v1.2.3 From 22877864c204139fe1c46899bedd237e38f0e849 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 23 Apr 2008 14:52:30 -0700 Subject: Add mmap ioctl to mmfs. --- linux-core/mmfs_drv.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'linux-core') diff --git a/linux-core/mmfs_drv.c b/linux-core/mmfs_drv.c index d973592d..88246297 100644 --- a/linux-core/mmfs_drv.c +++ b/linux-core/mmfs_drv.c @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include "mmfs.h" /** @file mmfs.c @@ -244,6 +246,45 @@ mmfs_pread_ioctl(struct inode *inode, struct file *filp, return 0; } +/** + * Maps the contents of an object, returning the address it is mapped + * into. + * + * While the mapping holds a reference on the contents of the object, it doesn't + * imply a ref on the object itself. + */ +static int +mmfs_mmap_ioctl(struct inode *inode, struct file *filp, + unsigned int cmd, unsigned long arg) +{ + struct mmfs_file *mmfs_filp = filp->private_data; + struct mmfs_mmap_args args; + struct mmfs_object *obj; + loff_t offset; + + if (copy_from_user(&args, (void __user *)arg, sizeof(args))) + return -EFAULT; + + obj = mmfs_object_lookup(mmfs_filp, args.handle); + if (obj == NULL) + return -EINVAL; + + offset = args.offset; + + down_write(¤t->mm->mmap_sem); + args.addr = (void *)do_mmap(obj->filp, 0, args.size, + PROT_READ | PROT_WRITE, MAP_SHARED, + args.offset); + up_write(¤t->mm->mmap_sem); + + mmfs_object_unreference(obj); + + if (copy_to_user((void __user *)arg, &args, sizeof(args))) + return -EFAULT; + + return 0; +} + /** * Writes data to the object referenced by handle. * @@ -297,6 +338,8 @@ mmfs_ioctl(struct inode *inode, struct file *filp, return mmfs_pread_ioctl(inode, filp, cmd, arg); case MMFS_IOCTL_PWRITE: return mmfs_pwrite_ioctl(inode, filp, cmd, arg); + case MMFS_IOCTL_MMAP: + return mmfs_mmap_ioctl(inode, filp, cmd, arg); default: return -EINVAL; } -- cgit v1.2.3 From 3ad8db2071d30c198403e605f2726fc5c3e46bfd Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 28 Apr 2008 16:54:53 -0700 Subject: Rename drm_mm.c and its fuctions to drm_memrange. It's not really a graphics memory allocator, just something to track ranges of address space. It doesn't involve actual allocation, and was consuming some desired namespace. --- linux-core/Makefile.kernel | 2 +- linux-core/drmP.h | 43 ++++--- linux-core/drm_bo.c | 38 +++--- linux-core/drm_bo_move.c | 2 +- linux-core/drm_drv.c | 2 +- linux-core/drm_memrange.c | 296 +++++++++++++++++++++++++++++++++++++++++++++ linux-core/drm_mm.c | 296 --------------------------------------------- linux-core/drm_objects.h | 6 +- linux-core/drm_sman.c | 22 ++-- linux-core/drm_sman.h | 4 +- linux-core/drm_stub.c | 6 +- linux-core/nouveau_bo.c | 2 +- linux-core/nouveau_sgdma.c | 2 +- 13 files changed, 363 insertions(+), 358 deletions(-) create mode 100644 linux-core/drm_memrange.c delete mode 100644 linux-core/drm_mm.c (limited to 'linux-core') diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index 093552bf..c4975924 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -12,7 +12,7 @@ drm-objs := drm_auth.o drm_bufs.o drm_context.o drm_dma.o drm_drawable.o \ drm_lock.o drm_memory.o drm_proc.o drm_stub.o drm_vm.o \ drm_sysfs.o drm_pci.o drm_agpsupport.o drm_scatter.o \ drm_memory_debug.o ati_pcigart.o drm_sman.o \ - drm_hashtab.o drm_mm.o drm_object.o drm_compat.o \ + drm_hashtab.o drm_memrange.o drm_object.o drm_compat.o \ drm_fence.o drm_ttm.o drm_bo.o drm_bo_move.o drm_bo_lock.o \ drm_regman.o tdfx-objs := tdfx_drv.o diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 69d31e14..ea8a997f 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -539,17 +539,17 @@ struct drm_sigdata { * Generic memory manager structs */ -struct drm_mm_node { +struct drm_memrange_node { struct list_head fl_entry; struct list_head ml_entry; int free; unsigned long start; unsigned long size; - struct drm_mm *mm; + struct drm_memrange *mm; void *private; }; -struct drm_mm { +struct drm_memrange { struct list_head fl_entry; struct list_head ml_entry; }; @@ -563,7 +563,7 @@ struct drm_map_list { struct drm_hash_item hash; struct drm_map *map; /**< mapping */ uint64_t user_token; - struct drm_mm_node *file_offset_node; + struct drm_memrange_node *file_offset_node; }; typedef struct drm_map drm_local_map_t; @@ -787,7 +787,7 @@ struct drm_device { struct list_head maplist; /**< Linked list of regions */ int map_count; /**< Number of mappable regions */ struct drm_open_hash map_hash; /**< User token hash table for maps */ - struct drm_mm offset_manager; /**< User token manager */ + struct drm_memrange offset_manager; /**< User token manager */ struct drm_open_hash object_hash; /**< User token hash table for objects */ struct address_space *dev_mapping; /**< For unmap_mapping_range() */ struct page *ttm_dummy_page; @@ -1234,22 +1234,27 @@ extern int drm_sysfs_device_add(struct drm_minor *minor); extern void drm_sysfs_device_remove(struct drm_minor *minor); /* - * Basic memory manager support (drm_mm.c) + * Basic memory manager support (drm_memrange.c) */ -extern struct drm_mm_node * drm_mm_get_block(struct drm_mm_node * parent, unsigned long size, - unsigned alignment); -extern void drm_mm_put_block(struct drm_mm_node *cur); -extern struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm, unsigned long size, - unsigned alignment, int best_match); -extern int drm_mm_init(struct drm_mm *mm, unsigned long start, unsigned long size); -extern void drm_mm_takedown(struct drm_mm *mm); -extern int drm_mm_clean(struct drm_mm *mm); -extern unsigned long drm_mm_tail_space(struct drm_mm *mm); -extern int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size); -extern int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size); - -static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block) +extern struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * parent, + unsigned long size, + unsigned alignment); +extern void drm_memrange_put_block(struct drm_memrange_node *cur); +extern struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange *mm, + unsigned long size, + unsigned alignment, int best_match); +extern int drm_memrange_init(struct drm_memrange *mm, + unsigned long start, unsigned long size); +extern void drm_memrange_takedown(struct drm_memrange *mm); +extern int drm_memrange_clean(struct drm_memrange *mm); +extern unsigned long drm_memrange_tail_space(struct drm_memrange *mm); +extern int drm_memrange_remove_space_from_tail(struct drm_memrange *mm, + unsigned long size); +extern int drm_memrange_add_space_to_tail(struct drm_memrange *mm, + unsigned long size); + +static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) { return block->mm; } diff --git a/linux-core/drm_bo.c b/linux-core/drm_bo.c index 88b2ee66..3abbb8c4 100644 --- a/linux-core/drm_bo.c +++ b/linux-core/drm_bo.c @@ -418,14 +418,14 @@ static void drm_bo_cleanup_refs(struct drm_buffer_object *bo, int remove_all) if (!bo->fence) { list_del_init(&bo->lru); if (bo->mem.mm_node) { - drm_mm_put_block(bo->mem.mm_node); + drm_memrange_put_block(bo->mem.mm_node); if (bo->pinned_node == bo->mem.mm_node) bo->pinned_node = NULL; bo->mem.mm_node = NULL; } list_del_init(&bo->pinned_lru); if (bo->pinned_node) { - drm_mm_put_block(bo->pinned_node); + drm_memrange_put_block(bo->pinned_node); bo->pinned_node = NULL; } list_del_init(&bo->ddestroy); @@ -791,7 +791,7 @@ out: mutex_lock(&dev->struct_mutex); if (evict_mem.mm_node) { if (evict_mem.mm_node != bo->pinned_node) - drm_mm_put_block(evict_mem.mm_node); + drm_memrange_put_block(evict_mem.mm_node); evict_mem.mm_node = NULL; } drm_bo_add_to_lru(bo); @@ -810,7 +810,7 @@ static int drm_bo_mem_force_space(struct drm_device *dev, struct drm_bo_mem_reg *mem, uint32_t mem_type, int no_wait) { - struct drm_mm_node *node; + struct drm_memrange_node *node; struct drm_buffer_manager *bm = &dev->bm; struct drm_buffer_object *entry; struct drm_mem_type_manager *man = &bm->man[mem_type]; @@ -820,7 +820,7 @@ static int drm_bo_mem_force_space(struct drm_device *dev, mutex_lock(&dev->struct_mutex); do { - node = drm_mm_search_free(&man->manager, num_pages, + node = drm_memrange_search_free(&man->manager, num_pages, mem->page_alignment, 1); if (node) break; @@ -846,7 +846,7 @@ static int drm_bo_mem_force_space(struct drm_device *dev, return -ENOMEM; } - node = drm_mm_get_block(node, num_pages, mem->page_alignment); + node = drm_memrange_get_block(node, num_pages, mem->page_alignment); if (unlikely(!node)) { mutex_unlock(&dev->struct_mutex); return -ENOMEM; @@ -924,7 +924,7 @@ int drm_bo_mem_space(struct drm_buffer_object *bo, int type_found = 0; int type_ok = 0; int has_eagain = 0; - struct drm_mm_node *node = NULL; + struct drm_memrange_node *node = NULL; int ret; mem->mm_node = NULL; @@ -952,10 +952,10 @@ int drm_bo_mem_space(struct drm_buffer_object *bo, mutex_lock(&dev->struct_mutex); if (man->has_type && man->use_type) { type_found = 1; - node = drm_mm_search_free(&man->manager, mem->num_pages, + node = drm_memrange_search_free(&man->manager, mem->num_pages, mem->page_alignment, 1); if (node) - node = drm_mm_get_block(node, mem->num_pages, + node = drm_memrange_get_block(node, mem->num_pages, mem->page_alignment); } mutex_unlock(&dev->struct_mutex); @@ -1340,7 +1340,7 @@ out_unlock: if (ret || !move_unfenced) { if (mem.mm_node) { if (mem.mm_node != bo->pinned_node) - drm_mm_put_block(mem.mm_node); + drm_memrange_put_block(mem.mm_node); mem.mm_node = NULL; } drm_bo_add_to_lru(bo); @@ -1432,7 +1432,7 @@ static int drm_buffer_object_validate(struct drm_buffer_object *bo, if (bo->pinned_node != bo->mem.mm_node) { if (bo->pinned_node != NULL) - drm_mm_put_block(bo->pinned_node); + drm_memrange_put_block(bo->pinned_node); bo->pinned_node = bo->mem.mm_node; } @@ -1443,7 +1443,7 @@ static int drm_buffer_object_validate(struct drm_buffer_object *bo, mutex_lock(&dev->struct_mutex); if (bo->pinned_node != bo->mem.mm_node) - drm_mm_put_block(bo->pinned_node); + drm_memrange_put_block(bo->pinned_node); list_del_init(&bo->pinned_lru); bo->pinned_node = NULL; @@ -2082,7 +2082,7 @@ static int drm_bo_leave_list(struct drm_buffer_object *bo, if (bo->pinned_node == bo->mem.mm_node) bo->pinned_node = NULL; if (bo->pinned_node != NULL) { - drm_mm_put_block(bo->pinned_node); + drm_memrange_put_block(bo->pinned_node); bo->pinned_node = NULL; } mutex_unlock(&dev->struct_mutex); @@ -2223,8 +2223,8 @@ int drm_bo_clean_mm(struct drm_device *dev, unsigned mem_type, int kern_clean) drm_bo_force_list_clean(dev, &man->lru, mem_type, 1, 0, 0); drm_bo_force_list_clean(dev, &man->pinned, mem_type, 1, 0, 1); - if (drm_mm_clean(&man->manager)) { - drm_mm_takedown(&man->manager); + if (drm_memrange_clean(&man->manager)) { + drm_memrange_takedown(&man->manager); } else { ret = -EBUSY; } @@ -2295,7 +2295,7 @@ int drm_bo_init_mm(struct drm_device *dev, unsigned type, DRM_ERROR("Zero size memory manager type %d\n", type); return ret; } - ret = drm_mm_init(&man->manager, p_offset, p_size); + ret = drm_memrange_init(&man->manager, p_offset, p_size); if (ret) return ret; } @@ -2713,7 +2713,7 @@ static void drm_bo_takedown_vm_locked(struct drm_buffer_object *bo) list->user_token = 0; } if (list->file_offset_node) { - drm_mm_put_block(list->file_offset_node); + drm_memrange_put_block(list->file_offset_node); list->file_offset_node = NULL; } @@ -2756,7 +2756,7 @@ static int drm_bo_setup_vm_locked(struct drm_buffer_object *bo) atomic_inc(&bo->usage); map->handle = (void *)bo; - list->file_offset_node = drm_mm_search_free(&dev->offset_manager, + list->file_offset_node = drm_memrange_search_free(&dev->offset_manager, bo->mem.num_pages, 0, 0); if (unlikely(!list->file_offset_node)) { @@ -2764,7 +2764,7 @@ static int drm_bo_setup_vm_locked(struct drm_buffer_object *bo) return -ENOMEM; } - list->file_offset_node = drm_mm_get_block(list->file_offset_node, + list->file_offset_node = drm_memrange_get_block(list->file_offset_node, bo->mem.num_pages, 0); if (unlikely(!list->file_offset_node)) { diff --git a/linux-core/drm_bo_move.c b/linux-core/drm_bo_move.c index bf0e1b74..850be5a3 100644 --- a/linux-core/drm_bo_move.c +++ b/linux-core/drm_bo_move.c @@ -41,7 +41,7 @@ static void drm_bo_free_old_node(struct drm_buffer_object *bo) if (old_mem->mm_node && (old_mem->mm_node != bo->pinned_node)) { mutex_lock(&bo->dev->struct_mutex); - drm_mm_put_block(old_mem->mm_node); + drm_memrange_put_block(old_mem->mm_node); mutex_unlock(&bo->dev->struct_mutex); } old_mem->mm_node = NULL; diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index b8b8333e..b5bd3616 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -415,7 +415,7 @@ static void drm_cleanup(struct drm_device * dev) drm_ctxbitmap_cleanup(dev); drm_ht_remove(&dev->map_hash); - drm_mm_takedown(&dev->offset_manager); + drm_memrange_takedown(&dev->offset_manager); drm_ht_remove(&dev->object_hash); drm_put_minor(&dev->primary); diff --git a/linux-core/drm_memrange.c b/linux-core/drm_memrange.c new file mode 100644 index 00000000..0ae03655 --- /dev/null +++ b/linux-core/drm_memrange.c @@ -0,0 +1,296 @@ +/************************************************************************** + * + * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * + **************************************************************************/ + +/* + * Generic simple memory manager implementation. Intended to be used as a base + * class implementation for more advanced memory managers. + * + * Note that the algorithm used is quite simple and there might be substantial + * performance gains if a smarter free list is implemented. Currently it is just an + * unordered stack of free regions. This could easily be improved if an RB-tree + * is used instead. At least if we expect heavy fragmentation. + * + * Aligned allocations can also see improvement. + * + * Authors: + * Thomas Hellström + */ + +#include "drmP.h" +#include + +unsigned long drm_memrange_tail_space(struct drm_memrange *mm) +{ + struct list_head *tail_node; + struct drm_memrange_node *entry; + + tail_node = mm->ml_entry.prev; + entry = list_entry(tail_node, struct drm_memrange_node, ml_entry); + if (!entry->free) + return 0; + + return entry->size; +} + +int drm_memrange_remove_space_from_tail(struct drm_memrange *mm, unsigned long size) +{ + struct list_head *tail_node; + struct drm_memrange_node *entry; + + tail_node = mm->ml_entry.prev; + entry = list_entry(tail_node, struct drm_memrange_node, ml_entry); + if (!entry->free) + return -ENOMEM; + + if (entry->size <= size) + return -ENOMEM; + + entry->size -= size; + return 0; +} + + +static int drm_memrange_create_tail_node(struct drm_memrange *mm, + unsigned long start, + unsigned long size) +{ + struct drm_memrange_node *child; + + child = (struct drm_memrange_node *) + drm_ctl_alloc(sizeof(*child), DRM_MEM_MM); + if (!child) + return -ENOMEM; + + child->free = 1; + child->size = size; + child->start = start; + child->mm = mm; + + list_add_tail(&child->ml_entry, &mm->ml_entry); + list_add_tail(&child->fl_entry, &mm->fl_entry); + + return 0; +} + + +int drm_memrange_add_space_to_tail(struct drm_memrange *mm, unsigned long size) +{ + struct list_head *tail_node; + struct drm_memrange_node *entry; + + tail_node = mm->ml_entry.prev; + entry = list_entry(tail_node, struct drm_memrange_node, ml_entry); + if (!entry->free) { + return drm_memrange_create_tail_node(mm, entry->start + entry->size, size); + } + entry->size += size; + return 0; +} + +static struct drm_memrange_node *drm_memrange_split_at_start(struct drm_memrange_node *parent, + unsigned long size) +{ + struct drm_memrange_node *child; + + child = (struct drm_memrange_node *) + drm_ctl_alloc(sizeof(*child), DRM_MEM_MM); + if (!child) + return NULL; + + INIT_LIST_HEAD(&child->fl_entry); + + child->free = 0; + child->size = size; + child->start = parent->start; + child->mm = parent->mm; + + list_add_tail(&child->ml_entry, &parent->ml_entry); + INIT_LIST_HEAD(&child->fl_entry); + + parent->size -= size; + parent->start += size; + return child; +} + +struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * parent, + unsigned long size, unsigned alignment) +{ + + struct drm_memrange_node *align_splitoff = NULL; + struct drm_memrange_node *child; + unsigned tmp = 0; + + if (alignment) + tmp = parent->start % alignment; + + if (tmp) { + align_splitoff = drm_memrange_split_at_start(parent, alignment - tmp); + if (!align_splitoff) + return NULL; + } + + if (parent->size == size) { + list_del_init(&parent->fl_entry); + parent->free = 0; + return parent; + } else { + child = drm_memrange_split_at_start(parent, size); + } + + if (align_splitoff) + drm_memrange_put_block(align_splitoff); + + return child; +} + +/* + * Put a block. Merge with the previous and / or next block if they are free. + * Otherwise add to the free stack. + */ + +void drm_memrange_put_block(struct drm_memrange_node * cur) +{ + + struct drm_memrange *mm = cur->mm; + struct list_head *cur_head = &cur->ml_entry; + struct list_head *root_head = &mm->ml_entry; + struct drm_memrange_node *prev_node = NULL; + struct drm_memrange_node *next_node; + + int merged = 0; + + if (cur_head->prev != root_head) { + prev_node = list_entry(cur_head->prev, struct drm_memrange_node, ml_entry); + if (prev_node->free) { + prev_node->size += cur->size; + merged = 1; + } + } + if (cur_head->next != root_head) { + next_node = list_entry(cur_head->next, struct drm_memrange_node, ml_entry); + if (next_node->free) { + if (merged) { + prev_node->size += next_node->size; + list_del(&next_node->ml_entry); + list_del(&next_node->fl_entry); + drm_ctl_free(next_node, sizeof(*next_node), + DRM_MEM_MM); + } else { + next_node->size += cur->size; + next_node->start = cur->start; + merged = 1; + } + } + } + if (!merged) { + cur->free = 1; + list_add(&cur->fl_entry, &mm->fl_entry); + } else { + list_del(&cur->ml_entry); + drm_ctl_free(cur, sizeof(*cur), DRM_MEM_MM); + } +} +EXPORT_SYMBOL(drm_memrange_put_block); + +struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange * mm, + unsigned long size, + unsigned alignment, int best_match) +{ + struct list_head *list; + const struct list_head *free_stack = &mm->fl_entry; + struct drm_memrange_node *entry; + struct drm_memrange_node *best; + unsigned long best_size; + unsigned wasted; + + best = NULL; + best_size = ~0UL; + + list_for_each(list, free_stack) { + entry = list_entry(list, struct drm_memrange_node, fl_entry); + wasted = 0; + + if (entry->size < size) + continue; + + if (alignment) { + register unsigned tmp = entry->start % alignment; + if (tmp) + wasted += alignment - tmp; + } + + + if (entry->size >= size + wasted) { + if (!best_match) + return entry; + if (size < best_size) { + best = entry; + best_size = entry->size; + } + } + } + + return best; +} + +int drm_memrange_clean(struct drm_memrange * mm) +{ + struct list_head *head = &mm->ml_entry; + + return (head->next->next == head); +} + +int drm_memrange_init(struct drm_memrange * mm, unsigned long start, unsigned long size) +{ + INIT_LIST_HEAD(&mm->ml_entry); + INIT_LIST_HEAD(&mm->fl_entry); + + return drm_memrange_create_tail_node(mm, start, size); +} + +EXPORT_SYMBOL(drm_memrange_init); + +void drm_memrange_takedown(struct drm_memrange * mm) +{ + struct list_head *bnode = mm->fl_entry.next; + struct drm_memrange_node *entry; + + entry = list_entry(bnode, struct drm_memrange_node, fl_entry); + + if (entry->ml_entry.next != &mm->ml_entry || + entry->fl_entry.next != &mm->fl_entry) { + DRM_ERROR("Memory manager not clean. Delaying takedown\n"); + return; + } + + list_del(&entry->fl_entry); + list_del(&entry->ml_entry); + drm_ctl_free(entry, sizeof(*entry), DRM_MEM_MM); +} + +EXPORT_SYMBOL(drm_memrange_takedown); diff --git a/linux-core/drm_mm.c b/linux-core/drm_mm.c deleted file mode 100644 index 59110293..00000000 --- a/linux-core/drm_mm.c +++ /dev/null @@ -1,296 +0,0 @@ -/************************************************************************** - * - * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL - * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR - * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE - * USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * - **************************************************************************/ - -/* - * Generic simple memory manager implementation. Intended to be used as a base - * class implementation for more advanced memory managers. - * - * Note that the algorithm used is quite simple and there might be substantial - * performance gains if a smarter free list is implemented. Currently it is just an - * unordered stack of free regions. This could easily be improved if an RB-tree - * is used instead. At least if we expect heavy fragmentation. - * - * Aligned allocations can also see improvement. - * - * Authors: - * Thomas Hellström - */ - -#include "drmP.h" -#include - -unsigned long drm_mm_tail_space(struct drm_mm *mm) -{ - struct list_head *tail_node; - struct drm_mm_node *entry; - - tail_node = mm->ml_entry.prev; - entry = list_entry(tail_node, struct drm_mm_node, ml_entry); - if (!entry->free) - return 0; - - return entry->size; -} - -int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size) -{ - struct list_head *tail_node; - struct drm_mm_node *entry; - - tail_node = mm->ml_entry.prev; - entry = list_entry(tail_node, struct drm_mm_node, ml_entry); - if (!entry->free) - return -ENOMEM; - - if (entry->size <= size) - return -ENOMEM; - - entry->size -= size; - return 0; -} - - -static int drm_mm_create_tail_node(struct drm_mm *mm, - unsigned long start, - unsigned long size) -{ - struct drm_mm_node *child; - - child = (struct drm_mm_node *) - drm_ctl_alloc(sizeof(*child), DRM_MEM_MM); - if (!child) - return -ENOMEM; - - child->free = 1; - child->size = size; - child->start = start; - child->mm = mm; - - list_add_tail(&child->ml_entry, &mm->ml_entry); - list_add_tail(&child->fl_entry, &mm->fl_entry); - - return 0; -} - - -int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size) -{ - struct list_head *tail_node; - struct drm_mm_node *entry; - - tail_node = mm->ml_entry.prev; - entry = list_entry(tail_node, struct drm_mm_node, ml_entry); - if (!entry->free) { - return drm_mm_create_tail_node(mm, entry->start + entry->size, size); - } - entry->size += size; - return 0; -} - -static struct drm_mm_node *drm_mm_split_at_start(struct drm_mm_node *parent, - unsigned long size) -{ - struct drm_mm_node *child; - - child = (struct drm_mm_node *) - drm_ctl_alloc(sizeof(*child), DRM_MEM_MM); - if (!child) - return NULL; - - INIT_LIST_HEAD(&child->fl_entry); - - child->free = 0; - child->size = size; - child->start = parent->start; - child->mm = parent->mm; - - list_add_tail(&child->ml_entry, &parent->ml_entry); - INIT_LIST_HEAD(&child->fl_entry); - - parent->size -= size; - parent->start += size; - return child; -} - -struct drm_mm_node *drm_mm_get_block(struct drm_mm_node * parent, - unsigned long size, unsigned alignment) -{ - - struct drm_mm_node *align_splitoff = NULL; - struct drm_mm_node *child; - unsigned tmp = 0; - - if (alignment) - tmp = parent->start % alignment; - - if (tmp) { - align_splitoff = drm_mm_split_at_start(parent, alignment - tmp); - if (!align_splitoff) - return NULL; - } - - if (parent->size == size) { - list_del_init(&parent->fl_entry); - parent->free = 0; - return parent; - } else { - child = drm_mm_split_at_start(parent, size); - } - - if (align_splitoff) - drm_mm_put_block(align_splitoff); - - return child; -} - -/* - * Put a block. Merge with the previous and / or next block if they are free. - * Otherwise add to the free stack. - */ - -void drm_mm_put_block(struct drm_mm_node * cur) -{ - - struct drm_mm *mm = cur->mm; - struct list_head *cur_head = &cur->ml_entry; - struct list_head *root_head = &mm->ml_entry; - struct drm_mm_node *prev_node = NULL; - struct drm_mm_node *next_node; - - int merged = 0; - - if (cur_head->prev != root_head) { - prev_node = list_entry(cur_head->prev, struct drm_mm_node, ml_entry); - if (prev_node->free) { - prev_node->size += cur->size; - merged = 1; - } - } - if (cur_head->next != root_head) { - next_node = list_entry(cur_head->next, struct drm_mm_node, ml_entry); - if (next_node->free) { - if (merged) { - prev_node->size += next_node->size; - list_del(&next_node->ml_entry); - list_del(&next_node->fl_entry); - drm_ctl_free(next_node, sizeof(*next_node), - DRM_MEM_MM); - } else { - next_node->size += cur->size; - next_node->start = cur->start; - merged = 1; - } - } - } - if (!merged) { - cur->free = 1; - list_add(&cur->fl_entry, &mm->fl_entry); - } else { - list_del(&cur->ml_entry); - drm_ctl_free(cur, sizeof(*cur), DRM_MEM_MM); - } -} -EXPORT_SYMBOL(drm_mm_put_block); - -struct drm_mm_node *drm_mm_search_free(const struct drm_mm * mm, - unsigned long size, - unsigned alignment, int best_match) -{ - struct list_head *list; - const struct list_head *free_stack = &mm->fl_entry; - struct drm_mm_node *entry; - struct drm_mm_node *best; - unsigned long best_size; - unsigned wasted; - - best = NULL; - best_size = ~0UL; - - list_for_each(list, free_stack) { - entry = list_entry(list, struct drm_mm_node, fl_entry); - wasted = 0; - - if (entry->size < size) - continue; - - if (alignment) { - register unsigned tmp = entry->start % alignment; - if (tmp) - wasted += alignment - tmp; - } - - - if (entry->size >= size + wasted) { - if (!best_match) - return entry; - if (size < best_size) { - best = entry; - best_size = entry->size; - } - } - } - - return best; -} - -int drm_mm_clean(struct drm_mm * mm) -{ - struct list_head *head = &mm->ml_entry; - - return (head->next->next == head); -} - -int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size) -{ - INIT_LIST_HEAD(&mm->ml_entry); - INIT_LIST_HEAD(&mm->fl_entry); - - return drm_mm_create_tail_node(mm, start, size); -} - -EXPORT_SYMBOL(drm_mm_init); - -void drm_mm_takedown(struct drm_mm * mm) -{ - struct list_head *bnode = mm->fl_entry.next; - struct drm_mm_node *entry; - - entry = list_entry(bnode, struct drm_mm_node, fl_entry); - - if (entry->ml_entry.next != &mm->ml_entry || - entry->fl_entry.next != &mm->fl_entry) { - DRM_ERROR("Memory manager not clean. Delaying takedown\n"); - return; - } - - list_del(&entry->fl_entry); - list_del(&entry->ml_entry); - drm_ctl_free(entry, sizeof(*entry), DRM_MEM_MM); -} - -EXPORT_SYMBOL(drm_mm_takedown); diff --git a/linux-core/drm_objects.h b/linux-core/drm_objects.h index 6a900612..6ec09ef8 100644 --- a/linux-core/drm_objects.h +++ b/linux-core/drm_objects.h @@ -418,7 +418,7 @@ extern int drm_ttm_destroy(struct drm_ttm *ttm); */ struct drm_bo_mem_reg { - struct drm_mm_node *mm_node; + struct drm_memrange_node *mm_node; unsigned long size; unsigned long num_pages; uint32_t page_alignment; @@ -499,7 +499,7 @@ struct drm_buffer_object { unsigned long num_pages; /* For pinned buffers */ - struct drm_mm_node *pinned_node; + struct drm_memrange_node *pinned_node; uint32_t pinned_mem_type; struct list_head pinned_lru; @@ -534,7 +534,7 @@ struct drm_mem_type_manager { int has_type; int use_type; int kern_init_type; - struct drm_mm manager; + struct drm_memrange manager; struct list_head lru; struct list_head pinned; uint32_t flags; diff --git a/linux-core/drm_sman.c b/linux-core/drm_sman.c index 8421a939..7c16f685 100644 --- a/linux-core/drm_sman.c +++ b/linux-core/drm_sman.c @@ -88,34 +88,34 @@ EXPORT_SYMBOL(drm_sman_init); static void *drm_sman_mm_allocate(void *private, unsigned long size, unsigned alignment) { - struct drm_mm *mm = (struct drm_mm *) private; - struct drm_mm_node *tmp; + struct drm_memrange *mm = (struct drm_memrange *) private; + struct drm_memrange_node *tmp; - tmp = drm_mm_search_free(mm, size, alignment, 1); + tmp = drm_memrange_search_free(mm, size, alignment, 1); if (!tmp) { return NULL; } - tmp = drm_mm_get_block(tmp, size, alignment); + tmp = drm_memrange_get_block(tmp, size, alignment); return tmp; } static void drm_sman_mm_free(void *private, void *ref) { - struct drm_mm_node *node = (struct drm_mm_node *) ref; + struct drm_memrange_node *node = (struct drm_memrange_node *) ref; - drm_mm_put_block(node); + drm_memrange_put_block(node); } static void drm_sman_mm_destroy(void *private) { - struct drm_mm *mm = (struct drm_mm *) private; - drm_mm_takedown(mm); + struct drm_memrange *mm = (struct drm_memrange *) private; + drm_memrange_takedown(mm); drm_free(mm, sizeof(*mm), DRM_MEM_MM); } static unsigned long drm_sman_mm_offset(void *private, void *ref) { - struct drm_mm_node *node = (struct drm_mm_node *) ref; + struct drm_memrange_node *node = (struct drm_memrange_node *) ref; return node->start; } @@ -124,7 +124,7 @@ drm_sman_set_range(struct drm_sman * sman, unsigned int manager, unsigned long start, unsigned long size) { struct drm_sman_mm *sman_mm; - struct drm_mm *mm; + struct drm_memrange *mm; int ret; BUG_ON(manager >= sman->num_managers); @@ -135,7 +135,7 @@ drm_sman_set_range(struct drm_sman * sman, unsigned int manager, return -ENOMEM; } sman_mm->private = mm; - ret = drm_mm_init(mm, start, size); + ret = drm_memrange_init(mm, start, size); if (ret) { drm_free(mm, sizeof(*mm), DRM_MEM_MM); diff --git a/linux-core/drm_sman.h b/linux-core/drm_sman.h index 39a39fef..0299776c 100644 --- a/linux-core/drm_sman.h +++ b/linux-core/drm_sman.h @@ -45,7 +45,7 @@ /* * A class that is an abstration of a simple memory allocator. * The sman implementation provides a default such allocator - * using the drm_mm.c implementation. But the user can replace it. + * using the drm_memrange.c implementation. But the user can replace it. * See the SiS implementation, which may use the SiS FB kernel module * for memory management. */ @@ -116,7 +116,7 @@ extern int drm_sman_init(struct drm_sman * sman, unsigned int num_managers, unsigned int user_order, unsigned int owner_order); /* - * Initialize a drm_mm.c allocator. Should be called only once for each + * Initialize a drm_memrange.c allocator. Should be called only once for each * manager unless a customized allogator is used. */ diff --git a/linux-core/drm_stub.c b/linux-core/drm_stub.c index c68adbaf..030fea54 100644 --- a/linux-core/drm_stub.c +++ b/linux-core/drm_stub.c @@ -115,15 +115,15 @@ static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev, if (drm_ht_create(&dev->map_hash, DRM_MAP_HASH_ORDER)) { return -ENOMEM; } - if (drm_mm_init(&dev->offset_manager, DRM_FILE_PAGE_OFFSET_START, - DRM_FILE_PAGE_OFFSET_SIZE)) { + if (drm_memrange_init(&dev->offset_manager, DRM_FILE_PAGE_OFFSET_START, + DRM_FILE_PAGE_OFFSET_SIZE)) { drm_ht_remove(&dev->map_hash); return -ENOMEM; } if (drm_ht_create(&dev->object_hash, DRM_OBJECT_HASH_ORDER)) { drm_ht_remove(&dev->map_hash); - drm_mm_takedown(&dev->offset_manager); + drm_memrange_takedown(&dev->offset_manager); return -ENOMEM; } diff --git a/linux-core/nouveau_bo.c b/linux-core/nouveau_bo.c index ab3b23a4..86347e03 100644 --- a/linux-core/nouveau_bo.c +++ b/linux-core/nouveau_bo.c @@ -229,7 +229,7 @@ out_cleanup: if (tmp_mem.mm_node) { mutex_lock(&dev->struct_mutex); if (tmp_mem.mm_node != bo->pinned_node) - drm_mm_put_block(tmp_mem.mm_node); + drm_memrange_put_block(tmp_mem.mm_node); tmp_mem.mm_node = NULL; mutex_unlock(&dev->struct_mutex); } diff --git a/linux-core/nouveau_sgdma.c b/linux-core/nouveau_sgdma.c index cc4d5a92..81704ea1 100644 --- a/linux-core/nouveau_sgdma.c +++ b/linux-core/nouveau_sgdma.c @@ -280,7 +280,7 @@ nouveau_sgdma_nottm_hack_init(struct drm_device *dev) struct drm_nouveau_private *dev_priv = dev->dev_private; struct drm_ttm_backend *be; struct drm_scatter_gather sgreq; - struct drm_mm_node mm_node; + struct drm_memrange_node mm_node; struct drm_bo_mem_reg mem; int ret; -- cgit v1.2.3 From dabd056bf34b389585b618cf03a297877505f06b Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 29 Apr 2008 13:30:44 -0700 Subject: Move mmfs ioctls into the DRM. Untested. --- linux-core/Makefile.kernel | 2 +- linux-core/drmP.h | 43 +++++- linux-core/drm_drv.c | 6 + linux-core/drm_fops.c | 6 + linux-core/drm_mm.c | 359 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 414 insertions(+), 2 deletions(-) create mode 100644 linux-core/drm_mm.c (limited to 'linux-core') diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index c4975924..4580a738 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -14,7 +14,7 @@ drm-objs := drm_auth.o drm_bufs.o drm_context.o drm_dma.o drm_drawable.o \ drm_memory_debug.o ati_pcigart.o drm_sman.o \ drm_hashtab.o drm_memrange.o drm_object.o drm_compat.o \ drm_fence.o drm_ttm.o drm_bo.o drm_bo_move.o drm_bo_lock.o \ - drm_regman.o + drm_regman.o drm_mm.o tdfx-objs := tdfx_drv.o r128-objs := r128_drv.o r128_cce.o r128_state.o r128_irq.o mga-objs := mga_drv.o mga_dma.o mga_state.o mga_warp.o mga_irq.o diff --git a/linux-core/drmP.h b/linux-core/drmP.h index ea8a997f..29dd4321 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -107,7 +107,7 @@ struct drm_file; #define DRIVER_IRQ_SHARED 0x80 #define DRIVER_DMA_QUEUE 0x100 #define DRIVER_FB_DMA 0x200 - +#define DRIVER_MM 0x400 /*@}*/ @@ -427,6 +427,11 @@ struct drm_file { struct list_head refd_objects; + /** Mapping of mm object handles to object pointers. */ + struct idr object_idr; + /** Lock for synchronization of access to object_idr. */ + spinlock_t table_lock; + struct drm_open_hash refd_object_hash[_DRM_NO_REF_TYPES]; struct file *filp; void *driver_priv; @@ -604,6 +609,26 @@ struct drm_ati_pcigart_info { int table_size; }; +/** + * This structure defines the drm_mm memory object, which will be used by the + * DRM for its buffer objects. + */ +struct drm_mm_object { + /** File representing the shmem storage */ + struct file *filp; + + spinlock_t lock; + + /** + * Size of the object, in bytes. Immutable over the object's + * lifetime. + */ + size_t size; + + /** Reference count of this object, protected by object_lock */ + int refcount; +}; + #include "drm_objects.h" /** @@ -1259,6 +1284,22 @@ static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) return block->mm; } +/* Memory manager (drm_mm.c) */ +void drm_mm_object_reference(struct drm_mm_object *obj); +void drm_mm_object_unreference(struct drm_mm_object *obj); +int drm_mm_alloc_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_mm_unreference_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_mm_pread_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_mm_pwrite_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_mm_mmap_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +void drm_mm_open(struct drm_file *file_private); +void drm_mm_release(struct drm_file *file_private); + extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev); extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev); diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index b5bd3616..2847f51b 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -150,6 +150,12 @@ static struct drm_ioctl_desc drm_ioctls[] = { DRM_IOCTL_DEF(DRM_IOCTL_BO_VERSION, drm_bo_version_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_MM_INFO, drm_mm_info_ioctl, 0), + + DRM_IOCTL_DEF(DRM_IOCTL_MM_ALLOC, drm_mm_alloc_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_MM_UNREFERENCE, drm_mm_unreference_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_MM_PREAD, drm_mm_pread_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_MM_PWRITE, drm_mm_pwrite_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_MM_MMAP, drm_mm_mmap_ioctl, 0), }; #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls ) diff --git a/linux-core/drm_fops.c b/linux-core/drm_fops.c index a4c76f75..2213230c 100644 --- a/linux-core/drm_fops.c +++ b/linux-core/drm_fops.c @@ -274,6 +274,9 @@ static int drm_open_helper(struct inode *inode, struct file *filp, goto out_free; } + if (dev->driver->driver_features & DRIVER_MM) + drm_mm_open(priv); + if (dev->driver->open) { ret = dev->driver->open(dev, priv); if (ret < 0) @@ -447,6 +450,9 @@ int drm_release(struct inode *inode, struct file *filp) dev->driver->reclaim_buffers(dev, file_priv); } + if (dev->driver->driver_features & DRIVER_MM) + drm_mm_release(file_priv); + drm_fasync(-1, filp, 0); mutex_lock(&dev->ctxlist_mutex); diff --git a/linux-core/drm_mm.c b/linux-core/drm_mm.c new file mode 100644 index 00000000..7e9fe39c --- /dev/null +++ b/linux-core/drm_mm.c @@ -0,0 +1,359 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "drmP.h" + +/** @file drm_mm.c + * + * This file provides some of the base ioctls and library routines for + * the graphics memory manager implemented by each device driver. + * + * Because various devices have different requirements in terms of + * synchronization and migration strategies, implementing that is left up to + * the driver, and all that the general API provides should be generic -- + * allocating objects, reading/writing data with the cpu, freeing objects. + * Even there, platform-dependent optimizations for reading/writing data with + * the CPU mean we'll likely hook those out to driver-specific calls. However, + * the DRI2 implementation wants to have at least allocate/mmap be generic. + * + * The goal was to have swap-backed object allocation managed through + * struct file. However, file descriptors as handles to a struct file have + * two major failings: + * - Process limits prevent more than 1024 or so being used at a time by + * default. + * - Inability to allocate high fds will aggravate the X Server's select() + * handling, and likely that of many GL client applications as well. + * + * This led to a plan of using our own integer IDs (called handles, following + * DRM terminology) to mimic fds, and implement the fd syscalls we need as + * ioctls. The objects themselves will still include the struct file so + * that we can transition to fds if the required kernel infrastructure shows + * up at a later data, and as our interface with shmfs for memory allocation. + */ + +static struct drm_mm_object * +drm_mm_object_alloc(size_t size) +{ + struct drm_mm_object *obj; + + BUG_ON((size & (PAGE_SIZE - 1)) != 0); + + obj = kcalloc(1, sizeof(*obj), GFP_KERNEL); + + obj->filp = shmem_file_setup("drm mm object", size, 0); + if (IS_ERR(obj->filp)) { + kfree(obj); + return NULL; + } + + obj->refcount = 1; + + return obj; +} + +/** + * Removes the mapping from handle to filp for this object. + */ +static int +drm_mm_handle_delete(struct drm_file *filp, int handle) +{ + struct drm_mm_object *obj; + + /* This is gross. The idr system doesn't let us try a delete and + * return an error code. It just spews if you fail at deleting. + * So, we have to grab a lock around finding the object and then + * doing the delete on it and dropping the refcount, or the user + * could race us to double-decrement the refcount and cause a + * use-after-free later. Given the frequency of our handle lookups, + * we may want to use ida for number allocation and a hash table + * for the pointers, anyway. + */ + spin_lock(&filp->table_lock); + + /* Check if we currently have a reference on the object */ + obj = idr_find(&filp->object_idr, handle); + if (obj == NULL) { + spin_unlock(&filp->table_lock); + return -EINVAL; + } + + /* Release reference and decrement refcount. */ + idr_remove(&filp->object_idr, handle); + drm_mm_object_unreference(obj); + + spin_unlock(&filp->table_lock); + + return 0; +} + +/** Returns a reference to the object named by the handle. */ +static struct drm_mm_object * +drm_mm_object_lookup(struct drm_file *filp, int handle) +{ + struct drm_mm_object *obj; + + spin_lock(&filp->table_lock); + + /* Check if we currently have a reference on the object */ + obj = idr_find(&filp->object_idr, handle); + if (obj == NULL) { + spin_unlock(&filp->table_lock); + return NULL; + } + + drm_mm_object_reference(obj); + + spin_unlock(&filp->table_lock); + + return obj; +} + + +/** + * Allocates a new mm object and returns a handle to it. + */ +int +drm_mm_alloc_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_mm_alloc_args *args = data; + struct drm_mm_object *obj; + int handle, ret; + + /* Round requested size up to page size */ + args->size = (args->size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + + /* Allocate the new object */ + obj = drm_mm_object_alloc(args->size); + if (obj == NULL) + return -ENOMEM; + + /* Get the user-visible handle using idr. + * + * I'm not really sure why the idr api needs us to do this in two + * repeating steps. It handles internal locking of its data + * structure, yet insists that we keep its memory allocation step + * separate from its slot-finding step for locking purposes. + */ + do { + if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) { + kfree(obj); + return -EFAULT; + } + + ret = idr_get_new(&file_priv->object_idr, obj, &handle); + } while (ret == -EAGAIN); + + if (ret != 0) { + drm_mm_object_unreference(obj); + return -EFAULT; + } + + args->handle = handle; + + return 0; +} + +/** + * Releases the handle to an mm object. + */ +int +drm_mm_unreference_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_mm_unreference_args *args = data; + int ret; + + ret = drm_mm_handle_delete(file_priv, args->handle); + + return ret; +} + +/** + * Reads data from the object referenced by handle. + * + * On error, the contents of *data are undefined. + */ +int +drm_mm_pread_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_mm_pread_args *args = data; + struct drm_mm_object *obj; + ssize_t read; + loff_t offset; + + obj = drm_mm_object_lookup(file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + offset = args->offset; + + read = obj->filp->f_op->read(obj->filp, (char __user *)args->data, + args->size, &offset); + if (read != args->size) { + drm_mm_object_unreference(obj); + if (read < 0) + return read; + else + return -EINVAL; + } + + drm_mm_object_unreference(obj); + + return 0; +} + +/** + * Maps the contents of an object, returning the address it is mapped + * into. + * + * While the mapping holds a reference on the contents of the object, it doesn't + * imply a ref on the object itself. + */ +int +drm_mm_mmap_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_mm_mmap_args *args = data; + struct drm_mm_object *obj; + loff_t offset; + + obj = drm_mm_object_lookup(file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + offset = args->offset; + + down_write(¤t->mm->mmap_sem); + args->addr = (void *)do_mmap(obj->filp, 0, args->size, + PROT_READ | PROT_WRITE, MAP_SHARED, + args->offset); + up_write(¤t->mm->mmap_sem); + + drm_mm_object_unreference(obj); + + return 0; +} + +/** + * Writes data to the object referenced by handle. + * + * On error, the contents of the buffer that were to be modified are undefined. + */ +int +drm_mm_pwrite_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_mm_pwrite_args *args = data; + struct drm_mm_object *obj; + ssize_t written; + loff_t offset; + + obj = drm_mm_object_lookup(file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + offset = args->offset; + + written = obj->filp->f_op->write(obj->filp, (char __user *)args->data, + args->size, &offset); + if (written != args->size) { + drm_mm_object_unreference(obj); + if (written < 0) + return written; + else + return -EINVAL; + } + + drm_mm_object_unreference(obj); + + return 0; +} + +/** + * Called at device open time, sets up the structure for handling refcounting + * of mm objects. + */ +void +drm_mm_open(struct drm_file *file_private) +{ + idr_init(&file_private->object_idr); +} + +/** Called at device close to release the file's references on objects. */ +static int +drm_mm_object_release(int id, void *ptr, void *data) +{ + struct drm_mm_object *obj = ptr; + + drm_mm_object_unreference(obj); + + return 0; +} + +/** + * Called at close time when the filp is going away. + * + * Releases any remaining references on objects by this filp. + */ +void +drm_mm_release(struct drm_file *file_private) +{ + idr_for_each(&file_private->object_idr, &drm_mm_object_release, NULL); + + idr_destroy(&file_private->object_idr); +} + +void +drm_mm_object_reference(struct drm_mm_object *obj) +{ + spin_lock(&obj->lock); + obj->refcount++; + spin_unlock(&obj->lock); +} + +void +drm_mm_object_unreference(struct drm_mm_object *obj) +{ + spin_lock(&obj->lock); + obj->refcount--; + spin_unlock(&obj->lock); + if (obj->refcount == 0) { + fput(obj->filp); + kfree(obj); + } +} -- cgit v1.2.3 From 81ba8ded7e01b21e600069977e496017c8966d66 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 29 Apr 2008 13:47:12 -0700 Subject: Remove the remainder of the mmfs device. --- linux-core/Makefile | 3 +- linux-core/Makefile.kernel | 3 +- linux-core/mmfs.h | 1 - linux-core/mmfs_drv.c | 447 --------------------------------------------- linux-core/mmfs_drv.h | 62 ------- 5 files changed, 2 insertions(+), 514 deletions(-) delete mode 120000 linux-core/mmfs.h delete mode 100644 linux-core/mmfs_drv.c delete mode 100644 linux-core/mmfs_drv.h (limited to 'linux-core') diff --git a/linux-core/Makefile b/linux-core/Makefile index f2d44642..3af6f370 100644 --- a/linux-core/Makefile +++ b/linux-core/Makefile @@ -58,7 +58,7 @@ endif # Modules for all architectures MODULE_LIST := drm.o tdfx.o r128.o radeon.o mga.o sis.o savage.o via.o \ - mach64.o nv.o nouveau.o xgi.o mmfs.o + mach64.o nv.o nouveau.o xgi.o # Modules only for ix86 architectures ifneq (,$(findstring 86,$(MACHINE))) @@ -92,7 +92,6 @@ NVHEADERS = nv_drv.h $(DRMHEADERS) FFBHEADERS = ffb_drv.h $(DRMHEADERS) NOUVEAUHEADERS = nouveau_drv.h nouveau_drm.h nouveau_reg.h $(DRMHEADERS) XGIHEADERS = xgi_cmdlist.h xgi_drv.h xgi_misc.h xgi_regs.h $(DRMHEADERS) -MMFSHEADERS = mmfs.h mmfs_priv.h PROGS = dristat drmstat diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index 4580a738..5b309bc1 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -41,7 +41,6 @@ mach64-objs := mach64_drv.o mach64_dma.o mach64_irq.o mach64_state.o nv-objs := nv_drv.o xgi-objs := xgi_cmdlist.o xgi_drv.o xgi_fb.o xgi_misc.o xgi_pcie.o \ xgi_fence.o -mmfs-objs := mmfs_drv.o ifeq ($(CONFIG_COMPAT),y) drm-objs += drm_ioc32.o @@ -53,7 +52,7 @@ nouveau-objs += nouveau_ioc32.o xgi-objs += xgi_ioc32.o endif -obj-m += drm.o mmfs.o +obj-m += drm.o obj-$(CONFIG_DRM_TDFX) += tdfx.o obj-$(CONFIG_DRM_R128) += r128.o obj-$(CONFIG_DRM_RADEON)+= radeon.o diff --git a/linux-core/mmfs.h b/linux-core/mmfs.h deleted file mode 120000 index 3e589504..00000000 --- a/linux-core/mmfs.h +++ /dev/null @@ -1 +0,0 @@ -../shared-core/mmfs.h \ No newline at end of file diff --git a/linux-core/mmfs_drv.c b/linux-core/mmfs_drv.c deleted file mode 100644 index 88246297..00000000 --- a/linux-core/mmfs_drv.c +++ /dev/null @@ -1,447 +0,0 @@ -/* - * Copyright © 2008 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - * - * Authors: - * Eric Anholt - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "mmfs.h" - -/** @file mmfs.c - * - * This file provides the filesystem for memory manager objects used by the - * DRM. - * - * The goal is to have swap-backed object allocation managed through - * struct file. However, file descriptors as handles to a struct file have - * two major failings: - * - Process limits prevent more than 1024 or so being used at a time by - * default. - * - Inability to allocate high fds will aggravate the X Server's select() - * handling, and likely that of many GL client applications as well. - * - * This led to a plan of using our own integer IDs (called handles, following - * DRM terminology) to mimic fds, and implement the fd syscalls we need as - * ioctls. The objects themselves will still include the struct file so - * that we can transition to fds if the required kernel infrastructure shows - * up at a later data, and as our interface with shmfs for memory allocation. - */ - -static struct mmfs_object * -mmfs_object_alloc(size_t size) -{ - struct mmfs_object *obj; - - BUG_ON((size & (PAGE_SIZE - 1)) != 0); - - obj = kcalloc(1, sizeof(*obj), GFP_KERNEL); - - obj->filp = shmem_file_setup("mmfs object", size, 0); - if (IS_ERR(obj->filp)) { - kfree(obj); - return NULL; - } - - obj->refcount = 1; - - return obj; -} - -/** - * Removes the mapping from handle to filp for this object. - */ -static int -mmfs_handle_delete(struct mmfs_file *mmfs_filp, int handle) -{ - struct mmfs_object *obj; - - /* This is gross. The idr system doesn't let us try a delete and - * return an error code. It just spews if you fail at deleting. - * So, we have to grab a lock around finding the object and then - * doing the delete on it and dropping the refcount, or the user - * could race us to double-decrement the refcount and cause a - * use-after-free later. Given the frequency of our handle lookups, - * we may want to use ida for number allocation and a hash table - * for the pointers, anyway. - */ - spin_lock(&mmfs_filp->table_lock); - - /* Check if we currently have a reference on the object */ - obj = idr_find(&mmfs_filp->object_idr, handle); - if (obj == NULL) { - spin_unlock(&mmfs_filp->table_lock); - return -EINVAL; - } - - /* Release reference and decrement refcount. */ - idr_remove(&mmfs_filp->object_idr, handle); - mmfs_object_unreference(obj); - - spin_unlock(&mmfs_filp->table_lock); - - return 0; -} - -/** Returns a reference to the object named by the handle. */ -static struct mmfs_object * -mmfs_object_lookup(struct mmfs_file *mmfs_filp, int handle) -{ - struct mmfs_object *obj; - - spin_lock(&mmfs_filp->table_lock); - - /* Check if we currently have a reference on the object */ - obj = idr_find(&mmfs_filp->object_idr, handle); - if (obj == NULL) { - spin_unlock(&mmfs_filp->table_lock); - return NULL; - } - - mmfs_object_reference(obj); - - spin_unlock(&mmfs_filp->table_lock); - - return obj; -} - - -/** - * Allocates a new mmfs object and returns a handle to it. - */ -static int -mmfs_alloc_ioctl(struct inode *inode, struct file *filp, - unsigned int cmd, unsigned long arg) -{ - struct mmfs_file *mmfs_filp = filp->private_data; - struct mmfs_alloc_args args; - struct mmfs_object *obj; - int handle, ret; - - if (copy_from_user(&args, (void __user *)arg, sizeof(args))) - return -EFAULT; - - /* Round requested size up to page size */ - args.size = (args.size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); - - /* Allocate the new object */ - obj = mmfs_object_alloc(args.size); - if (obj == NULL) - return -ENOMEM; - - /* Get the user-visible handle using idr. - * - * I'm not really sure why the idr api needs us to do this in two - * repeating steps. It handles internal locking of its data - * structure, yet insists that we keep its memory allocation step - * separate from its slot-finding step for locking purposes. - */ - do { - if (idr_pre_get(&mmfs_filp->object_idr, GFP_KERNEL) == 0) { - kfree(obj); - return -EFAULT; - } - - ret = idr_get_new(&mmfs_filp->object_idr, obj, &handle); - } while (ret == -EAGAIN); - - if (ret != 0) { - mmfs_object_unreference(obj); - return -EFAULT; - } - - args.handle = handle; - - if (copy_to_user((void __user *)arg, &args, sizeof(args))) { - mmfs_handle_delete(mmfs_filp, args.handle); - return -EFAULT; - } - - return 0; -} - -/** - * Releases the handle to an mmfs object. - */ -static int -mmfs_unreference_ioctl(struct inode *inode, struct file *filp, - unsigned int cmd, unsigned long arg) -{ - struct mmfs_file *mmfs_filp = filp->private_data; - struct mmfs_unreference_args args; - int ret; - - if (copy_from_user(&args, (void __user *)arg, sizeof(args))) - return -EFAULT; - - ret = mmfs_handle_delete(mmfs_filp, args.handle); - - return ret; -} - -/** - * Reads data from the object referenced by handle. - * - * On error, the contents of *data are undefined. - */ -static int -mmfs_pread_ioctl(struct inode *inode, struct file *filp, - unsigned int cmd, unsigned long arg) -{ - struct mmfs_file *mmfs_filp = filp->private_data; - struct mmfs_pread_args args; - struct mmfs_object *obj; - ssize_t read; - loff_t offset; - - if (copy_from_user(&args, (void __user *)arg, sizeof(args))) - return -EFAULT; - - obj = mmfs_object_lookup(mmfs_filp, args.handle); - if (obj == NULL) - return -EINVAL; - - offset = args.offset; - - read = obj->filp->f_op->read(obj->filp, (char __user *)args.data, - args.size, &offset); - if (read != args.size) { - mmfs_object_unreference(obj); - if (read < 0) - return read; - else - return -EINVAL; - } - - mmfs_object_unreference(obj); - - return 0; -} - -/** - * Maps the contents of an object, returning the address it is mapped - * into. - * - * While the mapping holds a reference on the contents of the object, it doesn't - * imply a ref on the object itself. - */ -static int -mmfs_mmap_ioctl(struct inode *inode, struct file *filp, - unsigned int cmd, unsigned long arg) -{ - struct mmfs_file *mmfs_filp = filp->private_data; - struct mmfs_mmap_args args; - struct mmfs_object *obj; - loff_t offset; - - if (copy_from_user(&args, (void __user *)arg, sizeof(args))) - return -EFAULT; - - obj = mmfs_object_lookup(mmfs_filp, args.handle); - if (obj == NULL) - return -EINVAL; - - offset = args.offset; - - down_write(¤t->mm->mmap_sem); - args.addr = (void *)do_mmap(obj->filp, 0, args.size, - PROT_READ | PROT_WRITE, MAP_SHARED, - args.offset); - up_write(¤t->mm->mmap_sem); - - mmfs_object_unreference(obj); - - if (copy_to_user((void __user *)arg, &args, sizeof(args))) - return -EFAULT; - - return 0; -} - -/** - * Writes data to the object referenced by handle. - * - * On error, the contents of the buffer that were to be modified are undefined. - */ -static int -mmfs_pwrite_ioctl(struct inode *inode, struct file *filp, - unsigned int cmd, unsigned long arg) -{ - struct mmfs_file *mmfs_filp = filp->private_data; - struct mmfs_pwrite_args args; - struct mmfs_object *obj; - ssize_t written; - loff_t offset; - - if (copy_from_user(&args, (void __user *)arg, sizeof(args))) - return -EFAULT; - - obj = mmfs_object_lookup(mmfs_filp, args.handle); - if (obj == NULL) - return -EINVAL; - - offset = args.offset; - - written = obj->filp->f_op->write(obj->filp, (char __user *)args.data, - args.size, &offset); - if (written != args.size) { - mmfs_object_unreference(obj); - if (written < 0) - return written; - else - return -EINVAL; - } - - mmfs_object_unreference(obj); - - return 0; -} - -static int -mmfs_ioctl(struct inode *inode, struct file *filp, - unsigned int cmd, unsigned long arg) -{ - - switch (cmd) { - case MMFS_IOCTL_ALLOC: - return mmfs_alloc_ioctl(inode, filp, cmd, arg); - case MMFS_IOCTL_UNREFERENCE: - return mmfs_unreference_ioctl(inode, filp, cmd, arg); - case MMFS_IOCTL_PREAD: - return mmfs_pread_ioctl(inode, filp, cmd, arg); - case MMFS_IOCTL_PWRITE: - return mmfs_pwrite_ioctl(inode, filp, cmd, arg); - case MMFS_IOCTL_MMAP: - return mmfs_mmap_ioctl(inode, filp, cmd, arg); - default: - return -EINVAL; - } -} - -/** - * Sets up the file private for keeping track of our mappings of handles to - * mmfs objects. - */ -int -mmfs_open(struct inode *inode, struct file *filp) -{ - struct mmfs_file *mmfs_filp; - - if (filp->f_flags & O_EXCL) - return -EBUSY; /* No exclusive opens */ - - mmfs_filp = kcalloc(1, sizeof(*mmfs_filp), GFP_KERNEL); - if (mmfs_filp == NULL) - return -ENOMEM; - filp->private_data = mmfs_filp; - - idr_init(&mmfs_filp->object_idr); - - return 0; -} - -/** Called at device close to release the file's references on objects. */ -static int -mmfs_object_release(int id, void *ptr, void *data) -{ - struct mmfs_object *obj = ptr; - - mmfs_object_unreference(obj); - - return 0; -} - -/** - * Called at close time when the filp is going away. - * - * Releases any remaining references on objects by this filp. - */ -int -mmfs_close(struct inode *inode, struct file *filp) -{ - struct mmfs_file *mmfs_filp = filp->private_data; - - idr_for_each(&mmfs_filp->object_idr, &mmfs_object_release, NULL); - - idr_destroy(&mmfs_filp->object_idr); - - kfree(mmfs_filp); - filp->private_data = NULL; - - return 0; -} - -void -mmfs_object_reference(struct mmfs_object *obj) -{ - spin_lock(&obj->lock); - obj->refcount++; - spin_unlock(&obj->lock); -} - -void -mmfs_object_unreference(struct mmfs_object *obj) -{ - spin_lock(&obj->lock); - obj->refcount--; - spin_unlock(&obj->lock); - if (obj->refcount == 0) { - fput(obj->filp); - kfree(obj); - } -} - -/** File operations structure */ -static const struct file_operations mmfs_dev_fops = { - .owner = THIS_MODULE, - .open = mmfs_open, - .release = mmfs_close, - .ioctl = mmfs_ioctl, -}; - -static int __init mmfs_init(void) -{ - int ret; - - ret = register_chrdev(MMFS_DEVICE_MAJOR, "mmfs", &mmfs_dev_fops); - if (ret != 0) - return ret; - - return 0; -} - -static void __exit mmfs_exit(void) -{ - unregister_chrdev(MMFS_DEVICE_MAJOR, "mmfs"); -} - -module_init(mmfs_init); -module_exit(mmfs_exit); -MODULE_LICENSE("GPL and additional rights"); diff --git a/linux-core/mmfs_drv.h b/linux-core/mmfs_drv.h deleted file mode 100644 index 53d2f6cb..00000000 --- a/linux-core/mmfs_drv.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright © 2008 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - * - * Authors: - * Eric Anholt - * - */ - -#include -#include - -/** @file mmfs_drv.h - * This file provides structure definitions and function prototypes for mmfs. - */ - -/** - * This structure defines the mmfs memory object, which will be used by the - * DRM for its buffer objects. - */ -struct mmfs_object { - /** File representing the shmem storage */ - struct file *filp; - - spinlock_t lock; - - size_t size; - /** Reference count of this object, protected by object_lock */ - int refcount; -}; - -/** - * This structure defines the process (actually per-fd) mapping of object - * handles to mmfs objects. - */ -struct mmfs_file { - /** Mapping of object handles to object pointers. */ - struct idr object_idr; - /** Lock for synchronization of access to object_idr. */ - spinlock_t table_lock; -}; - -void mmfs_object_reference(struct mmfs_object *obj); -void mmfs_object_unreference(struct mmfs_object *obj); -- cgit v1.2.3 From 1a8406795052e3ec49e400465f3211d04fd9dd86 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 30 Apr 2008 16:03:15 -0700 Subject: Hacking towards hooking up execbuffer. --- linux-core/Makefile.kernel | 2 +- linux-core/drmP.h | 4 ++++ linux-core/drm_memrange.c | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index 5b309bc1..97d53622 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -20,7 +20,7 @@ r128-objs := r128_drv.o r128_cce.o r128_state.o r128_irq.o mga-objs := mga_drv.o mga_dma.o mga_state.o mga_warp.o mga_irq.o i810-objs := i810_drv.o i810_dma.o i915-objs := i915_drv.o i915_dma.o i915_irq.o i915_mem.o i915_fence.o \ - i915_buffer.o i915_compat.o i915_execbuf.o + i915_buffer.o i915_compat.o i915_execbuf.o i915_mm.o nouveau-objs := nouveau_drv.o nouveau_state.o nouveau_fifo.o nouveau_mem.o \ nouveau_object.o nouveau_irq.o nouveau_notifier.o nouveau_swmthd.o \ nouveau_sgdma.o nouveau_dma.o nouveau_bo.o nouveau_fence.o \ diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 29dd4321..113cbecb 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -1278,6 +1278,10 @@ extern int drm_memrange_remove_space_from_tail(struct drm_memrange *mm, unsigned long size); extern int drm_memrange_add_space_to_tail(struct drm_memrange *mm, unsigned long size); +extern int drm_memrange_for_each(struct drm_memrange *mm, + int (*callback)(struct drm_memrange_node *node, + void *data), + void *data); static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) { diff --git a/linux-core/drm_memrange.c b/linux-core/drm_memrange.c index 0ae03655..e1d2233b 100644 --- a/linux-core/drm_memrange.c +++ b/linux-core/drm_memrange.c @@ -273,6 +273,31 @@ int drm_memrange_init(struct drm_memrange * mm, unsigned long start, unsigned lo return drm_memrange_create_tail_node(mm, start, size); } +/** + * Walks the list of allocated memory ranges and calls the callback on + * one. + */ +int drm_memrange_for_each(struct drm_memrange *mm, + int (*callback)(struct drm_memrange_node *node, + void *data), + void *data) +{ + struct list_head *list, *next; + + list_for_each_safe(list, next, &mm->ml_entry) { + struct drm_memrange_node *cur; + int ret; + + cur = list_entry(list, struct drm_memrange_node, ml_entry); + + ret = callback(cur, data); + if (ret != 0) + return ret; + } + + return 0; +} + EXPORT_SYMBOL(drm_memrange_init); void drm_memrange_takedown(struct drm_memrange * mm) -- cgit v1.2.3 From 2140e102f942edf7982cee2a3f00caf234551687 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 11:39:06 -0700 Subject: checkpoint: rename to GEM and a few more i915 bits. --- linux-core/Makefile.kernel | 4 +- linux-core/drmP.h | 45 ++++-- linux-core/drm_drv.c | 10 +- linux-core/drm_fops.c | 8 +- linux-core/drm_gem.c | 371 +++++++++++++++++++++++++++++++++++++++++++++ linux-core/drm_mm.c | 359 ------------------------------------------- linux-core/i915_drv.c | 2 + linux-core/i915_gem.c | 140 +++++++++++++++++ 8 files changed, 554 insertions(+), 385 deletions(-) create mode 100644 linux-core/drm_gem.c delete mode 100644 linux-core/drm_mm.c create mode 100644 linux-core/i915_gem.c (limited to 'linux-core') diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index 97d53622..82132ff4 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -14,13 +14,13 @@ drm-objs := drm_auth.o drm_bufs.o drm_context.o drm_dma.o drm_drawable.o \ drm_memory_debug.o ati_pcigart.o drm_sman.o \ drm_hashtab.o drm_memrange.o drm_object.o drm_compat.o \ drm_fence.o drm_ttm.o drm_bo.o drm_bo_move.o drm_bo_lock.o \ - drm_regman.o drm_mm.o + drm_regman.o drm_gem.o tdfx-objs := tdfx_drv.o r128-objs := r128_drv.o r128_cce.o r128_state.o r128_irq.o mga-objs := mga_drv.o mga_dma.o mga_state.o mga_warp.o mga_irq.o i810-objs := i810_drv.o i810_dma.o i915-objs := i915_drv.o i915_dma.o i915_irq.o i915_mem.o i915_fence.o \ - i915_buffer.o i915_compat.o i915_execbuf.o i915_mm.o + i915_buffer.o i915_compat.o i915_execbuf.o i915_gem.o nouveau-objs := nouveau_drv.o nouveau_state.o nouveau_fifo.o nouveau_mem.o \ nouveau_object.o nouveau_irq.o nouveau_notifier.o nouveau_swmthd.o \ nouveau_sgdma.o nouveau_dma.o nouveau_bo.o nouveau_fence.o \ diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 113cbecb..c582b80b 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -107,7 +107,7 @@ struct drm_file; #define DRIVER_IRQ_SHARED 0x80 #define DRIVER_DMA_QUEUE 0x100 #define DRIVER_FB_DMA 0x200 -#define DRIVER_MM 0x400 +#define DRIVER_GEM 0x400 /*@}*/ @@ -613,7 +613,7 @@ struct drm_ati_pcigart_info { * This structure defines the drm_mm memory object, which will be used by the * DRM for its buffer objects. */ -struct drm_mm_object { +struct drm_gem_object { /** File representing the shmem storage */ struct file *filp; @@ -627,6 +627,8 @@ struct drm_mm_object { /** Reference count of this object, protected by object_lock */ int refcount; + + void *driver_private; }; #include "drm_objects.h" @@ -730,6 +732,17 @@ struct drm_driver { void (*set_version) (struct drm_device *dev, struct drm_set_version *sv); + /** + * Driver-specific constructor for drm_gem_objects, to set up + * obj->driver_private. + * + * Returns 0 on success. + */ + int (*gem_init_object) (struct drm_device *dev, + struct drm_gem_object *obj); + void (*gem_free_object) (struct drm_device *dev, + struct drm_gem_object *obj); + struct drm_fence_driver *fence_driver; struct drm_bo_driver *bo_driver; @@ -1289,20 +1302,22 @@ static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) } /* Memory manager (drm_mm.c) */ -void drm_mm_object_reference(struct drm_mm_object *obj); -void drm_mm_object_unreference(struct drm_mm_object *obj); -int drm_mm_alloc_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); -int drm_mm_unreference_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); -int drm_mm_pread_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); -int drm_mm_pwrite_ioctl(struct drm_device *dev, void *data, +void drm_gem_object_reference(struct drm_device *dev, + struct drm_gem_object *obj); +void drm_gem_object_unreference(struct drm_device *dev, + struct drm_gem_object *obj); +int drm_gem_alloc_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); -int drm_mm_mmap_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); -void drm_mm_open(struct drm_file *file_private); -void drm_mm_release(struct drm_file *file_private); +int drm_gem_unreference_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_gem_pread_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_gem_mmap_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +void drm_gem_open(struct drm_device *dev, struct drm_file *file_private); +void drm_gem_release(struct drm_device *dev, struct drm_file *file_private); extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev); extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev); diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index 2847f51b..11fedad6 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -151,11 +151,11 @@ static struct drm_ioctl_desc drm_ioctls[] = { DRM_IOCTL_DEF(DRM_IOCTL_MM_INFO, drm_mm_info_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_MM_ALLOC, drm_mm_alloc_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_MM_UNREFERENCE, drm_mm_unreference_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_MM_PREAD, drm_mm_pread_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_MM_PWRITE, drm_mm_pwrite_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_MM_MMAP, drm_mm_mmap_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_ALLOC, drm_gem_alloc_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_UNREFERENCE, drm_gem_unreference_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_PREAD, drm_gem_pread_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_PWRITE, drm_gem_pwrite_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_MMAP, drm_gem_mmap_ioctl, 0), }; #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls ) diff --git a/linux-core/drm_fops.c b/linux-core/drm_fops.c index 2213230c..c42bfaab 100644 --- a/linux-core/drm_fops.c +++ b/linux-core/drm_fops.c @@ -274,8 +274,8 @@ static int drm_open_helper(struct inode *inode, struct file *filp, goto out_free; } - if (dev->driver->driver_features & DRIVER_MM) - drm_mm_open(priv); + if (dev->driver->driver_features & DRIVER_GEM) + drm_gem_open(dev, priv); if (dev->driver->open) { ret = dev->driver->open(dev, priv); @@ -450,8 +450,8 @@ int drm_release(struct inode *inode, struct file *filp) dev->driver->reclaim_buffers(dev, file_priv); } - if (dev->driver->driver_features & DRIVER_MM) - drm_mm_release(file_priv); + if (dev->driver->driver_features & DRIVER_GEM) + drm_gem_release(dev, file_priv); drm_fasync(-1, filp, 0); diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c new file mode 100644 index 00000000..bd51362e --- /dev/null +++ b/linux-core/drm_gem.c @@ -0,0 +1,371 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "drmP.h" + +/** @file drm_gem.c + * + * This file provides some of the base ioctls and library routines for + * the graphics memory manager implemented by each device driver. + * + * Because various devices have different requirements in terms of + * synchronization and migration strategies, implementing that is left up to + * the driver, and all that the general API provides should be generic -- + * allocating objects, reading/writing data with the cpu, freeing objects. + * Even there, platform-dependent optimizations for reading/writing data with + * the CPU mean we'll likely hook those out to driver-specific calls. However, + * the DRI2 implementation wants to have at least allocate/mmap be generic. + * + * The goal was to have swap-backed object allocation managed through + * struct file. However, file descriptors as handles to a struct file have + * two major failings: + * - Process limits prevent more than 1024 or so being used at a time by + * default. + * - Inability to allocate high fds will aggravate the X Server's select() + * handling, and likely that of many GL client applications as well. + * + * This led to a plan of using our own integer IDs (called handles, following + * DRM terminology) to mimic fds, and implement the fd syscalls we need as + * ioctls. The objects themselves will still include the struct file so + * that we can transition to fds if the required kernel infrastructure shows + * up at a later data, and as our interface with shmfs for memory allocation. + */ + +static struct drm_gem_object * +drm_gem_object_alloc(struct drm_device *dev, size_t size) +{ + struct drm_gem_object *obj; + + BUG_ON((size & (PAGE_SIZE - 1)) != 0); + + obj = kcalloc(1, sizeof(*obj), GFP_KERNEL); + + obj->filp = shmem_file_setup("drm mm object", size, 0); + if (IS_ERR(obj->filp)) { + kfree(obj); + return NULL; + } + + obj->refcount = 1; + + if (dev->driver->gem_init_object != NULL && + dev->driver->gem_init_object(dev, obj) != 0) { + fput(obj->filp); + kfree(obj); + return NULL; + } + return obj; +} + +/** + * Removes the mapping from handle to filp for this object. + */ +static int +drm_gem_handle_delete(struct drm_device *dev, struct drm_file *filp, + int handle) +{ + struct drm_gem_object *obj; + + /* This is gross. The idr system doesn't let us try a delete and + * return an error code. It just spews if you fail at deleting. + * So, we have to grab a lock around finding the object and then + * doing the delete on it and dropping the refcount, or the user + * could race us to double-decrement the refcount and cause a + * use-after-free later. Given the frequency of our handle lookups, + * we may want to use ida for number allocation and a hash table + * for the pointers, anyway. + */ + spin_lock(&filp->table_lock); + + /* Check if we currently have a reference on the object */ + obj = idr_find(&filp->object_idr, handle); + if (obj == NULL) { + spin_unlock(&filp->table_lock); + return -EINVAL; + } + + /* Release reference and decrement refcount. */ + idr_remove(&filp->object_idr, handle); + drm_gem_object_unreference(dev, obj); + + spin_unlock(&filp->table_lock); + + return 0; +} + +/** Returns a reference to the object named by the handle. */ +static struct drm_gem_object * +drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, + int handle) +{ + struct drm_gem_object *obj; + + spin_lock(&filp->table_lock); + + /* Check if we currently have a reference on the object */ + obj = idr_find(&filp->object_idr, handle); + if (obj == NULL) { + spin_unlock(&filp->table_lock); + return NULL; + } + + drm_gem_object_reference(dev, obj); + + spin_unlock(&filp->table_lock); + + return obj; +} + + +/** + * Allocates a new mm object and returns a handle to it. + */ +int +drm_gem_alloc_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_gem_alloc_args *args = data; + struct drm_gem_object *obj; + int handle, ret; + + /* Round requested size up to page size */ + args->size = (args->size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + + /* Allocate the new object */ + obj = drm_gem_object_alloc(dev, args->size); + if (obj == NULL) + return -ENOMEM; + + /* Get the user-visible handle using idr. + * + * I'm not really sure why the idr api needs us to do this in two + * repeating steps. It handles internal locking of its data + * structure, yet insists that we keep its memory allocation step + * separate from its slot-finding step for locking purposes. + */ + do { + if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) { + kfree(obj); + return -EFAULT; + } + + ret = idr_get_new(&file_priv->object_idr, obj, &handle); + } while (ret == -EAGAIN); + + if (ret != 0) { + drm_gem_object_unreference(dev, obj); + return -EFAULT; + } + + args->handle = handle; + + return 0; +} + +/** + * Releases the handle to an mm object. + */ +int +drm_gem_unreference_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_gem_unreference_args *args = data; + int ret; + + ret = drm_gem_handle_delete(dev, file_priv, args->handle); + + return ret; +} + +/** + * Reads data from the object referenced by handle. + * + * On error, the contents of *data are undefined. + */ +int +drm_gem_pread_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_gem_pread_args *args = data; + struct drm_gem_object *obj; + ssize_t read; + loff_t offset; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + offset = args->offset; + + read = obj->filp->f_op->read(obj->filp, (char __user *)args->data, + args->size, &offset); + if (read != args->size) { + drm_gem_object_unreference(dev, obj); + if (read < 0) + return read; + else + return -EINVAL; + } + + drm_gem_object_unreference(dev, obj); + + return 0; +} + +/** + * Maps the contents of an object, returning the address it is mapped + * into. + * + * While the mapping holds a reference on the contents of the object, it doesn't + * imply a ref on the object itself. + */ +int +drm_gem_mmap_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_gem_mmap_args *args = data; + struct drm_gem_object *obj; + loff_t offset; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + offset = args->offset; + + down_write(¤t->mm->mmap_sem); + args->addr = (void *)do_mmap(obj->filp, 0, args->size, + PROT_READ | PROT_WRITE, MAP_SHARED, + args->offset); + up_write(¤t->mm->mmap_sem); + + drm_gem_object_unreference(dev, obj); + + return 0; +} + +/** + * Writes data to the object referenced by handle. + * + * On error, the contents of the buffer that were to be modified are undefined. + */ +int +drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_gem_pwrite_args *args = data; + struct drm_gem_object *obj; + ssize_t written; + loff_t offset; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + offset = args->offset; + + written = obj->filp->f_op->write(obj->filp, (char __user *)args->data, + args->size, &offset); + if (written != args->size) { + drm_gem_object_unreference(dev, obj); + if (written < 0) + return written; + else + return -EINVAL; + } + + drm_gem_object_unreference(dev, obj); + + return 0; +} + +/** + * Called at device open time, sets up the structure for handling refcounting + * of mm objects. + */ +void +drm_gem_open(struct drm_device *dev, struct drm_file *file_private) +{ + idr_init(&file_private->object_idr); +} + +/** Called at device close to release the file's references on objects. */ +static int +drm_gem_object_release(int id, void *ptr, void *data) +{ + struct drm_device *dev = data; + struct drm_gem_object *obj = ptr; + + drm_gem_object_unreference(dev, obj); + + return 0; +} + +/** + * Called at close time when the filp is going away. + * + * Releases any remaining references on objects by this filp. + */ +void +drm_gem_release(struct drm_device *dev, struct drm_file *file_private) +{ + idr_for_each(&file_private->object_idr, &drm_gem_object_release, dev); + + idr_destroy(&file_private->object_idr); +} + +void +drm_gem_object_reference(struct drm_device *dev, struct drm_gem_object *obj) +{ + spin_lock(&obj->lock); + obj->refcount++; + spin_unlock(&obj->lock); +} + +void +drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj) +{ + spin_lock(&obj->lock); + obj->refcount--; + spin_unlock(&obj->lock); + if (obj->refcount == 0) { + if (dev->driver->gem_free_object != NULL) + dev->driver->gem_free_object(dev, obj); + + fput(obj->filp); + kfree(obj); + } +} diff --git a/linux-core/drm_mm.c b/linux-core/drm_mm.c deleted file mode 100644 index 7e9fe39c..00000000 --- a/linux-core/drm_mm.c +++ /dev/null @@ -1,359 +0,0 @@ -/* - * Copyright © 2008 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - * - * Authors: - * Eric Anholt - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "drmP.h" - -/** @file drm_mm.c - * - * This file provides some of the base ioctls and library routines for - * the graphics memory manager implemented by each device driver. - * - * Because various devices have different requirements in terms of - * synchronization and migration strategies, implementing that is left up to - * the driver, and all that the general API provides should be generic -- - * allocating objects, reading/writing data with the cpu, freeing objects. - * Even there, platform-dependent optimizations for reading/writing data with - * the CPU mean we'll likely hook those out to driver-specific calls. However, - * the DRI2 implementation wants to have at least allocate/mmap be generic. - * - * The goal was to have swap-backed object allocation managed through - * struct file. However, file descriptors as handles to a struct file have - * two major failings: - * - Process limits prevent more than 1024 or so being used at a time by - * default. - * - Inability to allocate high fds will aggravate the X Server's select() - * handling, and likely that of many GL client applications as well. - * - * This led to a plan of using our own integer IDs (called handles, following - * DRM terminology) to mimic fds, and implement the fd syscalls we need as - * ioctls. The objects themselves will still include the struct file so - * that we can transition to fds if the required kernel infrastructure shows - * up at a later data, and as our interface with shmfs for memory allocation. - */ - -static struct drm_mm_object * -drm_mm_object_alloc(size_t size) -{ - struct drm_mm_object *obj; - - BUG_ON((size & (PAGE_SIZE - 1)) != 0); - - obj = kcalloc(1, sizeof(*obj), GFP_KERNEL); - - obj->filp = shmem_file_setup("drm mm object", size, 0); - if (IS_ERR(obj->filp)) { - kfree(obj); - return NULL; - } - - obj->refcount = 1; - - return obj; -} - -/** - * Removes the mapping from handle to filp for this object. - */ -static int -drm_mm_handle_delete(struct drm_file *filp, int handle) -{ - struct drm_mm_object *obj; - - /* This is gross. The idr system doesn't let us try a delete and - * return an error code. It just spews if you fail at deleting. - * So, we have to grab a lock around finding the object and then - * doing the delete on it and dropping the refcount, or the user - * could race us to double-decrement the refcount and cause a - * use-after-free later. Given the frequency of our handle lookups, - * we may want to use ida for number allocation and a hash table - * for the pointers, anyway. - */ - spin_lock(&filp->table_lock); - - /* Check if we currently have a reference on the object */ - obj = idr_find(&filp->object_idr, handle); - if (obj == NULL) { - spin_unlock(&filp->table_lock); - return -EINVAL; - } - - /* Release reference and decrement refcount. */ - idr_remove(&filp->object_idr, handle); - drm_mm_object_unreference(obj); - - spin_unlock(&filp->table_lock); - - return 0; -} - -/** Returns a reference to the object named by the handle. */ -static struct drm_mm_object * -drm_mm_object_lookup(struct drm_file *filp, int handle) -{ - struct drm_mm_object *obj; - - spin_lock(&filp->table_lock); - - /* Check if we currently have a reference on the object */ - obj = idr_find(&filp->object_idr, handle); - if (obj == NULL) { - spin_unlock(&filp->table_lock); - return NULL; - } - - drm_mm_object_reference(obj); - - spin_unlock(&filp->table_lock); - - return obj; -} - - -/** - * Allocates a new mm object and returns a handle to it. - */ -int -drm_mm_alloc_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_mm_alloc_args *args = data; - struct drm_mm_object *obj; - int handle, ret; - - /* Round requested size up to page size */ - args->size = (args->size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); - - /* Allocate the new object */ - obj = drm_mm_object_alloc(args->size); - if (obj == NULL) - return -ENOMEM; - - /* Get the user-visible handle using idr. - * - * I'm not really sure why the idr api needs us to do this in two - * repeating steps. It handles internal locking of its data - * structure, yet insists that we keep its memory allocation step - * separate from its slot-finding step for locking purposes. - */ - do { - if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) { - kfree(obj); - return -EFAULT; - } - - ret = idr_get_new(&file_priv->object_idr, obj, &handle); - } while (ret == -EAGAIN); - - if (ret != 0) { - drm_mm_object_unreference(obj); - return -EFAULT; - } - - args->handle = handle; - - return 0; -} - -/** - * Releases the handle to an mm object. - */ -int -drm_mm_unreference_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_mm_unreference_args *args = data; - int ret; - - ret = drm_mm_handle_delete(file_priv, args->handle); - - return ret; -} - -/** - * Reads data from the object referenced by handle. - * - * On error, the contents of *data are undefined. - */ -int -drm_mm_pread_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_mm_pread_args *args = data; - struct drm_mm_object *obj; - ssize_t read; - loff_t offset; - - obj = drm_mm_object_lookup(file_priv, args->handle); - if (obj == NULL) - return -EINVAL; - - offset = args->offset; - - read = obj->filp->f_op->read(obj->filp, (char __user *)args->data, - args->size, &offset); - if (read != args->size) { - drm_mm_object_unreference(obj); - if (read < 0) - return read; - else - return -EINVAL; - } - - drm_mm_object_unreference(obj); - - return 0; -} - -/** - * Maps the contents of an object, returning the address it is mapped - * into. - * - * While the mapping holds a reference on the contents of the object, it doesn't - * imply a ref on the object itself. - */ -int -drm_mm_mmap_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_mm_mmap_args *args = data; - struct drm_mm_object *obj; - loff_t offset; - - obj = drm_mm_object_lookup(file_priv, args->handle); - if (obj == NULL) - return -EINVAL; - - offset = args->offset; - - down_write(¤t->mm->mmap_sem); - args->addr = (void *)do_mmap(obj->filp, 0, args->size, - PROT_READ | PROT_WRITE, MAP_SHARED, - args->offset); - up_write(¤t->mm->mmap_sem); - - drm_mm_object_unreference(obj); - - return 0; -} - -/** - * Writes data to the object referenced by handle. - * - * On error, the contents of the buffer that were to be modified are undefined. - */ -int -drm_mm_pwrite_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_mm_pwrite_args *args = data; - struct drm_mm_object *obj; - ssize_t written; - loff_t offset; - - obj = drm_mm_object_lookup(file_priv, args->handle); - if (obj == NULL) - return -EINVAL; - - offset = args->offset; - - written = obj->filp->f_op->write(obj->filp, (char __user *)args->data, - args->size, &offset); - if (written != args->size) { - drm_mm_object_unreference(obj); - if (written < 0) - return written; - else - return -EINVAL; - } - - drm_mm_object_unreference(obj); - - return 0; -} - -/** - * Called at device open time, sets up the structure for handling refcounting - * of mm objects. - */ -void -drm_mm_open(struct drm_file *file_private) -{ - idr_init(&file_private->object_idr); -} - -/** Called at device close to release the file's references on objects. */ -static int -drm_mm_object_release(int id, void *ptr, void *data) -{ - struct drm_mm_object *obj = ptr; - - drm_mm_object_unreference(obj); - - return 0; -} - -/** - * Called at close time when the filp is going away. - * - * Releases any remaining references on objects by this filp. - */ -void -drm_mm_release(struct drm_file *file_private) -{ - idr_for_each(&file_private->object_idr, &drm_mm_object_release, NULL); - - idr_destroy(&file_private->object_idr); -} - -void -drm_mm_object_reference(struct drm_mm_object *obj) -{ - spin_lock(&obj->lock); - obj->refcount++; - spin_unlock(&obj->lock); -} - -void -drm_mm_object_unreference(struct drm_mm_object *obj) -{ - spin_lock(&obj->lock); - obj->refcount--; - spin_unlock(&obj->lock); - if (obj->refcount == 0) { - fput(obj->filp); - kfree(obj); - } -} diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index 5a6f0adc..c77c329f 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -586,6 +586,8 @@ static struct drm_driver driver = { .get_map_ofs = drm_core_get_map_ofs, .get_reg_ofs = drm_core_get_reg_ofs, .ioctls = i915_ioctls, + .gem_init_object = i915_gem_init_object, + .gem_free_object = i915_gem_free_object, .fops = { .owner = THIS_MODULE, .open = drm_open, diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c new file mode 100644 index 00000000..0685a1b9 --- /dev/null +++ b/linux-core/i915_gem.c @@ -0,0 +1,140 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +#include "drmP.h" +#include "drm.h" +#include "i915_drm.h" +#include "i915_drv.h" + +int +i915_gem_init_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_init *args = data; + + if (args->gtt_start >= args->gtt_end || + (args->gtt_start & (PAGE_SIZE - 1)) != 0 || + (args->gtt_end & (PAGE_SIZE - 1)) != 0) + return -EINVAL; + + drm_memrange_init(&dev_priv->mm.gtt_space, args->gtt_start, + args->gtt_end); + + return 0; +} + +static void +i915_gem_evict_object(struct drm_device *dev, struct drm_gem_object *obj) +{ + +} + +static int +evict_callback(struct drm_memrange_node *node, void *data) +{ + struct drm_device *dev = data; + struct drm_gem_object *obj = node->private; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + if (obj_priv->pin_count == 0) + i915_gem_evict_object(dev, obj); + + return 0; +} + +int +i915_gem_execbuffer(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_execbuffer *args = data; + struct drm_i915_gem_validate_entry *validate_list; + int ret, i; + RING_LOCALS; + + LOCK_TEST_WITH_RETURN(dev, file_priv); + + /* Big hammer: flush and idle the hardware so we can map things in/out. + */ + BEGIN_LP_RING(2); + OUT_RING(CMD_MI_FLUSH | MI_READ_FLUSH | MI_EXE_FLUSH); + OUT_RING(0); /* noop */ + ADVANCE_LP_RING(); + ret = i915_quiescent(dev); + if (ret != 0) + return ret; + + /* Evict everything so we have space for sure. */ + drm_memrange_for_each(&dev_priv->mm.gtt_space, evict_callback); + + /* Copy in the validate list from userland */ + validate_list = drm_calloc(sizeof(*validate_list), args->buffer_count, + DRM_MEM_DRIVER); + ret = copy_from_user(validate_list, + (struct drm_i915_relocation_entry __user*) + args->buffers, + sizeof(*validate_list) * args->buffer_count); + if (ret != 0) { + drm_free(validate_list, + sizeof(*validate_list) * args->buffer_count, + DRM_MEM_DRIVER); + return ret; + } + + /* Perform the relocations */ + for (i = 0; i < args->buffer_count; i++) { + intel_gem_reloc_and_validate_buffer(dev, &validate_list[i]); + } + + /* Exec the batchbuffer */ + + + drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, + DRM_MEM_DRIVER); + + return 0; +} + +int i915_gem_init_object(struct drm_device *dev, struct drm_gem_object *obj) +{ + struct drm_i915_gem_object *obj_priv; + + obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER); + if (obj_priv == NULL) + return -ENOMEM; + + obj->driver_private = obj_priv; + + return 0; +} + +void i915_gem_free_object(struct drm_device *dev, struct drm_gem_object *obj) +{ + drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); +} + -- cgit v1.2.3 From 5af87acbc2025b9f72d51b30f176e9c3909695ac Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 14:20:44 -0700 Subject: checkpoint: gtt binding written. --- linux-core/drmP.h | 9 ++- linux-core/drm_agpsupport.c | 47 ++++++++++++ linux-core/drm_gem.c | 5 +- linux-core/i915_gem.c | 170 ++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 215 insertions(+), 16 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index c582b80b..2ed17b81 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -1038,6 +1038,10 @@ extern void drm_free_pages(unsigned long address, int order, int area); extern DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type); extern int drm_free_agp(DRM_AGP_MEM * handle, int pages); extern int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start); +extern DRM_AGP_MEM *drm_agp_bind_pages(struct drm_device *dev, + struct page **pages, + unsigned long num_pages, + uint32_t gtt_offset); extern int drm_unbind_agp(DRM_AGP_MEM * handle); extern void drm_free_memctl(size_t size); @@ -1301,11 +1305,14 @@ static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) return block->mm; } -/* Memory manager (drm_mm.c) */ +/* Graphics Execution Manager library functions (drm_gem.c) */ void drm_gem_object_reference(struct drm_device *dev, struct drm_gem_object *obj); void drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj); +struct drm_gem_object * +drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, + int handle); int drm_gem_alloc_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int drm_gem_unreference_ioctl(struct drm_device *dev, void *data, diff --git a/linux-core/drm_agpsupport.c b/linux-core/drm_agpsupport.c index 0aa94a75..b37d6d9d 100644 --- a/linux-core/drm_agpsupport.c +++ b/linux-core/drm_agpsupport.c @@ -484,6 +484,53 @@ int drm_agp_unbind_memory(DRM_AGP_MEM * handle) return agp_unbind_memory(handle); } +/** + * Binds a collection of pages into AGP memory at the given offset, returning + * the AGP memory structure containing them. + * + * No reference is held on the pages during this time -- it is up to the + * caller to handle that. + */ +DRM_AGP_MEM * +drm_agp_bind_pages(struct drm_device *dev, + struct page **pages, + unsigned long num_pages, + uint32_t gtt_offset) +{ + struct page **cur_page, **last_page = pages + num_pages; + DRM_AGP_MEM *mem; + int ret; + + DRM_DEBUG("drm_agp_populate_ttm\n"); +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11) + mem = drm_agp_allocate_memory(num_pages, AGP_USER_MEMORY); +#else + mem = drm_agp_allocate_memory(dev->agp->bridge, num_pages, + AGP_USER_MEMORY); +#endif + if (mem == NULL) { + DRM_ERROR("Failed to allocate memory for %ld pages\n", + num_pages); + return NULL; + } + + mem->page_count = 0; + for (cur_page = pages; cur_page < last_page; ++cur_page) { + struct page *page = *cur_page; + + mem->memory[mem->page_count++] = + phys_to_gart(page_to_phys(page)); + } + + mem->is_flushed = TRUE; + ret = drm_agp_bind_memory(mem, gtt_offset); + if (ret != 0) { + agp_free_memory(mem); + return NULL; + } + + return mem; +} /* diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index bd51362e..def526f0 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -127,7 +127,7 @@ drm_gem_handle_delete(struct drm_device *dev, struct drm_file *filp, } /** Returns a reference to the object named by the handle. */ -static struct drm_gem_object * +struct drm_gem_object * drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, int handle) { @@ -358,6 +358,9 @@ drm_gem_object_reference(struct drm_device *dev, struct drm_gem_object *obj) void drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj) { + if (obj == NULL) + return; + spin_lock(&obj->lock); obj->refcount--; spin_unlock(&obj->lock); diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 0685a1b9..bd030a88 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -49,9 +49,120 @@ i915_gem_init_ioctl(struct drm_device *dev, void *data, } static void -i915_gem_evict_object(struct drm_device *dev, struct drm_gem_object *obj) +i915_gem_object_free_page_list(struct drm_device *dev, + struct drm_gem_object *obj) { - + struct drm_i915_gem_object *obj_priv = obj->driver_private; + int page_count = obj->size / PAGE_SIZE; + int i; + + if (obj_priv->page_list == NULL) + return; + + /* Count how many we had successfully allocated, since release_pages() + * doesn't like NULLs. + */ + for (i = 0; i < obj->size / PAGE_SIZE; i++) { + if (obj_priv->page_list[i] == NULL) + break; + } + release_pages(obj_priv->page_list, i, 0); + + drm_free(obj_priv->page_list, + page_count * sizeof(struct page *), + DRM_MEM_DRIVER); + obj_priv->page_list = NULL; +} + +/** + * Unbinds an object from the GTT aperture. + */ +static void +i915_gem_object_unbind(struct drm_device *dev, struct drm_gem_object *obj) +{ + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + if (obj_priv->agp_mem != NULL) { + drm_unbind_agp(obj_priv->agp_mem); + drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); + } + + i915_gem_object_free_page_list(dev, obj); + + drm_memrange_put_block(obj_priv->gtt_space); +} + +/** + * Finds free space in the GTT aperture and binds the object there. + */ +static int +i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + struct drm_memrange_node *free_space; + int page_count, i; + + free_space = drm_memrange_search_free(&dev_priv->mm.gtt_space, + obj->size, + PAGE_SIZE, 0); + + obj_priv->gtt_space = drm_memrange_get_block(free_space, + obj->size, + PAGE_SIZE); + + /* Get the list of pages out of our struct file. They'll be pinned + * at this point until we release them. + */ + page_count = obj->size / PAGE_SIZE; + BUG_ON(obj_priv->page_list != NULL); + obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *), + DRM_MEM_DRIVER); + for (i = 0; i < page_count; i++) { + obj_priv->page_list[i] = + find_or_create_page(obj->filp->f_mapping, i, GFP_HIGHUSER); + + if (obj_priv->page_list[i] == NULL) { + i915_gem_object_free_page_list(dev, obj); + return -ENOMEM; + } + } + + /* Create an AGP memory structure pointing at our pages, and bind it + * into the GTT. + */ + obj_priv->agp_mem = drm_agp_bind_pages(dev, + obj_priv->page_list, + page_count, + obj_priv->gtt_offset); + if (obj_priv->agp_mem == NULL) { + i915_gem_object_free_page_list(dev, obj); + return -ENOMEM; + } + + return 0; +} + +static int +i915_gem_reloc_and_validate_object(struct drm_device *dev, + struct drm_file *file_priv, + struct drm_i915_gem_validate_entry *entry, + struct drm_gem_object *obj) +{ + struct drm_i915_gem_reloc *relocs; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + /* Walk the list of relocations and perform them if necessary. */ + /* XXX */ + + /* Choose the GTT offset for our buffer and put it there. */ + if (obj_priv->gtt_space == NULL) { + i915_gem_object_bind_to_gtt(dev, obj); + if (obj_priv->gtt_space == NULL) + return -ENOMEM; + } + + return 0; } static int @@ -62,18 +173,19 @@ evict_callback(struct drm_memrange_node *node, void *data) struct drm_i915_gem_object *obj_priv = obj->driver_private; if (obj_priv->pin_count == 0) - i915_gem_evict_object(dev, obj); + i915_gem_object_unbind(dev, obj); return 0; } int i915_gem_execbuffer(struct drm_device *dev, void *data, - struct drm_file *file_priv) + struct drm_file *file_priv) { drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_execbuffer *args = data; struct drm_i915_gem_validate_entry *validate_list; + struct drm_gem_object **object_list; int ret, i; RING_LOCALS; @@ -90,34 +202,64 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, return ret; /* Evict everything so we have space for sure. */ - drm_memrange_for_each(&dev_priv->mm.gtt_space, evict_callback); + drm_memrange_for_each(&dev_priv->mm.gtt_space, evict_callback, dev); /* Copy in the validate list from userland */ validate_list = drm_calloc(sizeof(*validate_list), args->buffer_count, DRM_MEM_DRIVER); + object_list = drm_calloc(sizeof(*object_list), args->buffer_count, + DRM_MEM_DRIVER); + if (validate_list == NULL || object_list == NULL) { + ret = -ENOMEM; + goto err; + } ret = copy_from_user(validate_list, (struct drm_i915_relocation_entry __user*) args->buffers, sizeof(*validate_list) * args->buffer_count); - if (ret != 0) { - drm_free(validate_list, - sizeof(*validate_list) * args->buffer_count, - DRM_MEM_DRIVER); - return ret; - } + if (ret != 0) + goto err; - /* Perform the relocations */ + /* Look up object handles and perform the relocations */ for (i = 0; i < args->buffer_count; i++) { - intel_gem_reloc_and_validate_buffer(dev, &validate_list[i]); + object_list[i] = drm_gem_object_lookup(dev, file_priv, + validate_list[i].buffer_handle); + if (object_list[i] == NULL) { + ret = -EINVAL; + goto err; + } + + i915_gem_reloc_and_validate_object(dev, file_priv, + &validate_list[i], + object_list[i]); } /* Exec the batchbuffer */ + /* Copy the new buffer offsets back to the user's validate list. */ + for (i = 0; i < args->buffer_count; i++) { + struct drm_i915_gem_object *obj_priv = + object_list[i]->driver_private; + validate_list[i].buffer_offset = obj_priv->gtt_offset; + } + ret = copy_to_user(validate_list, + (struct drm_i915_relocation_entry __user*) + args->buffers, + sizeof(*validate_list) * args->buffer_count); + + /* Clean up and return */ +err: + if (object_list != NULL) { + for (i = 0; i < args->buffer_count; i++) + drm_gem_object_unreference(dev, object_list[i]); + } + drm_free(object_list, sizeof(*object_list) * args->buffer_count, + DRM_MEM_DRIVER); drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, DRM_MEM_DRIVER); - return 0; + return ret; } int i915_gem_init_object(struct drm_device *dev, struct drm_gem_object *obj) -- cgit v1.2.3 From ccd1bae0f676490a88240c62f02e072d2cf3b030 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 15:22:21 -0700 Subject: checkpoint: relocations support. --- linux-core/drm_agpsupport.c | 8 ++-- linux-core/drm_gem.c | 4 +- linux-core/drm_memory.c | 2 + linux-core/drm_memrange.c | 3 ++ linux-core/i915_gem.c | 104 ++++++++++++++++++++++++++++++++++++-------- 5 files changed, 97 insertions(+), 24 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_agpsupport.c b/linux-core/drm_agpsupport.c index b37d6d9d..15400386 100644 --- a/linux-core/drm_agpsupport.c +++ b/linux-core/drm_agpsupport.c @@ -493,9 +493,9 @@ int drm_agp_unbind_memory(DRM_AGP_MEM * handle) */ DRM_AGP_MEM * drm_agp_bind_pages(struct drm_device *dev, - struct page **pages, - unsigned long num_pages, - uint32_t gtt_offset) + struct page **pages, + unsigned long num_pages, + uint32_t gtt_offset) { struct page **cur_page, **last_page = pages + num_pages; DRM_AGP_MEM *mem; @@ -531,7 +531,7 @@ drm_agp_bind_pages(struct drm_device *dev, return mem; } - +EXPORT_SYMBOL(drm_agp_bind_pages); /* * AGP ttm backend interface. diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index def526f0..09c0fe85 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -148,7 +148,7 @@ drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, return obj; } - +EXPORT_SYMBOL(drm_gem_object_lookup); /** * Allocates a new mm object and returns a handle to it. @@ -354,6 +354,7 @@ drm_gem_object_reference(struct drm_device *dev, struct drm_gem_object *obj) obj->refcount++; spin_unlock(&obj->lock); } +EXPORT_SYMBOL(drm_gem_object_reference); void drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj) @@ -372,3 +373,4 @@ drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj) kfree(obj); } } +EXPORT_SYMBOL(drm_gem_object_unreference); diff --git a/linux-core/drm_memory.c b/linux-core/drm_memory.c index 75f5b521..4b494f9c 100644 --- a/linux-core/drm_memory.c +++ b/linux-core/drm_memory.c @@ -310,6 +310,7 @@ int drm_free_agp(DRM_AGP_MEM * handle, int pages) { return drm_agp_free_memory(handle) ? 0 : -EINVAL; } +EXPORT_SYMBOL(drm_free_agp); /** Wrapper around agp_bind_memory() */ int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start) @@ -322,6 +323,7 @@ int drm_unbind_agp(DRM_AGP_MEM * handle) { return drm_agp_unbind_memory(handle); } +EXPORT_SYMBOL(drm_unbind_agp); #else /* __OS_HAS_AGP*/ static void *agp_remap(unsigned long offset, unsigned long size, diff --git a/linux-core/drm_memrange.c b/linux-core/drm_memrange.c index e1d2233b..663943ab 100644 --- a/linux-core/drm_memrange.c +++ b/linux-core/drm_memrange.c @@ -167,6 +167,7 @@ struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * pare return child; } +EXPORT_SYMBOL(drm_memrange_get_block); /* * Put a block. Merge with the previous and / or next block if they are free. @@ -257,6 +258,7 @@ struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange * m return best; } +EXPORT_SYMBOL(drm_memrange_search_free); int drm_memrange_clean(struct drm_memrange * mm) { @@ -297,6 +299,7 @@ int drm_memrange_for_each(struct drm_memrange *mm, return 0; } +EXPORT_SYMBOL(drm_memrange_for_each); EXPORT_SYMBOL(drm_memrange_init); diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index bd030a88..3e4403c7 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -59,14 +59,10 @@ i915_gem_object_free_page_list(struct drm_device *dev, if (obj_priv->page_list == NULL) return; - /* Count how many we had successfully allocated, since release_pages() - * doesn't like NULLs. - */ for (i = 0; i < obj->size / PAGE_SIZE; i++) { if (obj_priv->page_list[i] == NULL) - break; + put_page(obj_priv->page_list[i]); } - release_pages(obj_priv->page_list, i, 0); drm_free(obj_priv->page_list, page_count * sizeof(struct page *), @@ -149,11 +145,9 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, struct drm_i915_gem_validate_entry *entry, struct drm_gem_object *obj) { - struct drm_i915_gem_reloc *relocs; + struct drm_i915_gem_relocation_entry reloc; struct drm_i915_gem_object *obj_priv = obj->driver_private; - - /* Walk the list of relocations and perform them if necessary. */ - /* XXX */ + int i; /* Choose the GTT offset for our buffer and put it there. */ if (obj_priv->gtt_space == NULL) { @@ -162,6 +156,64 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, return -ENOMEM; } + /* Apply the relocations, using the GTT aperture to avoid cache + * flushing requirements. + */ + for (i = 0; i < entry->relocation_count; i++) { + struct drm_gem_object *target_obj; + struct drm_i915_gem_object *target_obj_priv; + void *reloc_page; + uint32_t reloc_val, *reloc_entry; + int ret; + + ret = copy_from_user(&reloc, entry->relocs + i, sizeof(reloc)); + if (ret != 0) + return ret; + + target_obj = drm_gem_object_lookup(dev, file_priv, + reloc.target_handle); + if (target_obj == NULL) + return -EINVAL; + target_obj_priv = target_obj->driver_private; + + /* The target buffer should have appeared before us in the + * validate list, so it should have a GTT space bound by now. + */ + if (target_obj_priv->gtt_space == NULL) { + DRM_ERROR("No GTT space found for object %d\n", + reloc.target_handle); + return -EINVAL; + } + + if (reloc.offset > obj->size - 4) { + DRM_ERROR("Relocation beyond object bounds.\n"); + return -EINVAL; + } + if (reloc.offset & 3) { + DRM_ERROR("Relocation not 4-byte aligned.\n"); + return -EINVAL; + } + + /* Map the page containing the relocation we're going to + * perform. + */ + reloc_page = ioremap(dev->agp->base + + (reloc.offset & ~(PAGE_SIZE - 1)), + PAGE_SIZE); + if (reloc_page == NULL) + return -ENOMEM; + + reloc_entry = (uint32_t *)((char *)reloc_page + + (reloc.offset & (PAGE_SIZE - 1))); + reloc_val = target_obj_priv->gtt_offset + reloc.delta; + + DRM_DEBUG("Applied relocation: %p@0x%08x = 0x%08x\n", + obj, reloc.offset, reloc_val); + *reloc_entry = reloc_val; + + iounmap(reloc_page); + } + return 0; } @@ -178,32 +230,44 @@ evict_callback(struct drm_memrange_node *node, void *data) return 0; } +static int +i915_gem_sync_and_evict(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + int ret; + RING_LOCALS; + + BEGIN_LP_RING(2); + OUT_RING(CMD_MI_FLUSH | MI_READ_FLUSH | MI_EXE_FLUSH); + OUT_RING(0); /* noop */ + ADVANCE_LP_RING(); + ret = i915_quiescent(dev); + if (ret != 0) + return ret; + + /* Evict everything so we have space for sure. */ + drm_memrange_for_each(&dev_priv->mm.gtt_space, evict_callback, dev); + + return 0; +} + int i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) { - drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_execbuffer *args = data; struct drm_i915_gem_validate_entry *validate_list; struct drm_gem_object **object_list; int ret, i; - RING_LOCALS; LOCK_TEST_WITH_RETURN(dev, file_priv); /* Big hammer: flush and idle the hardware so we can map things in/out. */ - BEGIN_LP_RING(2); - OUT_RING(CMD_MI_FLUSH | MI_READ_FLUSH | MI_EXE_FLUSH); - OUT_RING(0); /* noop */ - ADVANCE_LP_RING(); - ret = i915_quiescent(dev); + ret = i915_gem_sync_and_evict(dev); if (ret != 0) return ret; - /* Evict everything so we have space for sure. */ - drm_memrange_for_each(&dev_priv->mm.gtt_space, evict_callback, dev); - /* Copy in the validate list from userland */ validate_list = drm_calloc(sizeof(*validate_list), args->buffer_count, DRM_MEM_DRIVER); @@ -249,6 +313,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, sizeof(*validate_list) * args->buffer_count); /* Clean up and return */ + ret = i915_gem_sync_and_evict(dev); + err: if (object_list != NULL) { for (i = 0; i < args->buffer_count; i++) -- cgit v1.2.3 From 793549116ee6e9202fc7e474bd382eb19ffeb87f Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 15:40:02 -0700 Subject: Add pin/unpin object ioctls for gem. --- linux-core/i915_gem.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 3e4403c7..fc9e53cf 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -328,6 +328,48 @@ err: return ret; } +int +i915_gem_pin(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_pin *args = data; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + int ret; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + ret = i915_gem_object_bind_to_gtt(dev, obj); + if (ret != 0) + return ret; + + obj_priv = obj->driver_private; + obj_priv->pin_count++; + args->offset = obj_priv->gtt_offset; + + return 0; +} + +int +i915_gem_unpin(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_pin *args = data; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + obj_priv = obj->driver_private; + obj_priv->pin_count--; + + return 0; +} + int i915_gem_init_object(struct drm_device *dev, struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv; @@ -345,4 +387,3 @@ void i915_gem_free_object(struct drm_device *dev, struct drm_gem_object *obj) { drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); } - -- cgit v1.2.3 From d2529d13961f0df00754393e1ad9b72da5e998a4 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 16:27:03 -0700 Subject: Remove _args from gem ioctl argument structure tags. --- linux-core/drm_gem.c | 10 +++++----- linux-core/i915_gem.c | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 09c0fe85..7326d6bb 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -157,7 +157,7 @@ int drm_gem_alloc_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { - struct drm_gem_alloc_args *args = data; + struct drm_gem_alloc *args = data; struct drm_gem_object *obj; int handle, ret; @@ -202,7 +202,7 @@ int drm_gem_unreference_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { - struct drm_gem_unreference_args *args = data; + struct drm_gem_unreference *args = data; int ret; ret = drm_gem_handle_delete(dev, file_priv, args->handle); @@ -219,7 +219,7 @@ int drm_gem_pread_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { - struct drm_gem_pread_args *args = data; + struct drm_gem_pread *args = data; struct drm_gem_object *obj; ssize_t read; loff_t offset; @@ -256,7 +256,7 @@ int drm_gem_mmap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { - struct drm_gem_mmap_args *args = data; + struct drm_gem_mmap *args = data; struct drm_gem_object *obj; loff_t offset; @@ -286,7 +286,7 @@ int drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { - struct drm_gem_pwrite_args *args = data; + struct drm_gem_pwrite *args = data; struct drm_gem_object *obj; ssize_t written; loff_t offset; diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index fc9e53cf..09c94c3b 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -329,8 +329,8 @@ err: } int -i915_gem_pin(struct drm_device *dev, void *data, - struct drm_file *file_priv) +i915_gem_pin_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) { struct drm_i915_gem_pin *args = data; struct drm_gem_object *obj; @@ -353,8 +353,8 @@ i915_gem_pin(struct drm_device *dev, void *data, } int -i915_gem_unpin(struct drm_device *dev, void *data, - struct drm_file *file_priv) +i915_gem_unpin_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) { struct drm_i915_gem_pin *args = data; struct drm_gem_object *obj; -- cgit v1.2.3 From 7d5f783eca3302ec7efa164e2980c75e5e591585 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 16:38:37 -0700 Subject: Make GEM object handles be nonzero. --- linux-core/drm_gem.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 7326d6bb..f6038d5c 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -182,7 +182,8 @@ drm_gem_alloc_ioctl(struct drm_device *dev, void *data, return -EFAULT; } - ret = idr_get_new(&file_priv->object_idr, obj, &handle); + ret = idr_get_new_above(&file_priv->object_idr, obj, 1, + &handle); } while (ret == -EAGAIN); if (ret != 0) { -- cgit v1.2.3 From 3f641b56c79d48f7e11aa3eb1dc678e09c8e01f7 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 16:48:25 -0700 Subject: Fix missing member settings in obj/obj_priv, and some error paths. --- linux-core/drm_gem.c | 1 + linux-core/i915_gem.c | 7 +++++++ 2 files changed, 8 insertions(+) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index f6038d5c..ee3dbe47 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -80,6 +80,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) } obj->refcount = 1; + obj->size = size; if (dev->driver->gem_init_object != NULL && dev->driver->gem_init_object(dev, obj) != 0) { diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 09c94c3b..5ba48512 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -106,6 +106,9 @@ i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj) obj_priv->gtt_space = drm_memrange_get_block(free_space, obj->size, PAGE_SIZE); + obj_priv->gtt_offset = obj_priv->gtt_space->start; + + DRM_INFO("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); /* Get the list of pages out of our struct file. They'll be pinned * at this point until we release them. @@ -120,6 +123,8 @@ i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj) if (obj_priv->page_list[i] == NULL) { i915_gem_object_free_page_list(dev, obj); + drm_memrange_put_block(obj_priv->gtt_space); + obj_priv->gtt_space = NULL; return -ENOMEM; } } @@ -133,6 +138,8 @@ i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj) obj_priv->gtt_offset); if (obj_priv->agp_mem == NULL) { i915_gem_object_free_page_list(dev, obj); + drm_memrange_put_block(obj_priv->gtt_space); + obj_priv->gtt_space = NULL; return -ENOMEM; } -- cgit v1.2.3 From ddc80651d561ef3820e205255f34dc7c60dc2e7c Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 17:31:29 -0700 Subject: Fix offset passed to AGP to be pages instead of bytes. Fix some utterly bonged loop while we were staring at it. --- linux-core/drm_agpsupport.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_agpsupport.c b/linux-core/drm_agpsupport.c index 15400386..d6594b87 100644 --- a/linux-core/drm_agpsupport.c +++ b/linux-core/drm_agpsupport.c @@ -497,9 +497,8 @@ drm_agp_bind_pages(struct drm_device *dev, unsigned long num_pages, uint32_t gtt_offset) { - struct page **cur_page, **last_page = pages + num_pages; DRM_AGP_MEM *mem; - int ret; + int ret, i; DRM_DEBUG("drm_agp_populate_ttm\n"); #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11) @@ -514,17 +513,14 @@ drm_agp_bind_pages(struct drm_device *dev, return NULL; } - mem->page_count = 0; - for (cur_page = pages; cur_page < last_page; ++cur_page) { - struct page *page = *cur_page; - - mem->memory[mem->page_count++] = - phys_to_gart(page_to_phys(page)); - } + for (i = 0; i < num_pages; i++) + mem->memory[i] = phys_to_gart(page_to_phys(pages[i])); + mem->page_count = num_pages; mem->is_flushed = TRUE; - ret = drm_agp_bind_memory(mem, gtt_offset); + ret = drm_agp_bind_memory(mem, gtt_offset / PAGE_SIZE); if (ret != 0) { + DRM_ERROR("Failed to bind AGP memory: %d\n", ret); agp_free_memory(mem); return NULL; } -- cgit v1.2.3 From c10695bb7ab44494badc21c822eac3140cf4e117 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 17:31:57 -0700 Subject: Unbind objects when freeing, fix some error paths, and warn in others. --- linux-core/i915_gem.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 5ba48512..29b2d894 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -78,6 +78,9 @@ i915_gem_object_unbind(struct drm_device *dev, struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; + if (obj_priv->gtt_space == NULL) + return; + if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); @@ -86,6 +89,7 @@ i915_gem_object_unbind(struct drm_device *dev, struct drm_gem_object *obj) i915_gem_object_free_page_list(dev, obj); drm_memrange_put_block(obj_priv->gtt_space); + obj_priv->gtt_space = NULL; } /** @@ -102,13 +106,17 @@ i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj) free_space = drm_memrange_search_free(&dev_priv->mm.gtt_space, obj->size, PAGE_SIZE, 0); - + if (free_space == NULL) + return -ENOMEM; obj_priv->gtt_space = drm_memrange_get_block(free_space, obj->size, PAGE_SIZE); + if (obj_priv->gtt_space == NULL) + return -ENOMEM; + obj_priv->gtt_offset = obj_priv->gtt_space->start; - DRM_INFO("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); + DRM_DEBUG("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); /* Get the list of pages out of our struct file. They'll be pinned * at this point until we release them. @@ -117,6 +125,12 @@ i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj) BUG_ON(obj_priv->page_list != NULL); obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *), DRM_MEM_DRIVER); + if (obj_priv->page_list == NULL) { + drm_memrange_put_block(obj_priv->gtt_space); + obj_priv->gtt_space = NULL; + return -ENOMEM; + } + for (i = 0; i < page_count; i++) { obj_priv->page_list[i] = find_or_create_page(obj->filp->f_mapping, i, GFP_HIGHUSER); @@ -345,12 +359,18 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, int ret; obj = drm_gem_object_lookup(dev, file_priv, args->handle); - if (obj == NULL) + if (obj == NULL) { + DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n", + args->handle); return -EINVAL; + } ret = i915_gem_object_bind_to_gtt(dev, obj); - if (ret != 0) + if (ret != 0) { + DRM_ERROR("Failure to bind in i915_gem_pin_ioctl(): %d\n", + ret); return ret; + } obj_priv = obj->driver_private; obj_priv->pin_count++; @@ -368,8 +388,11 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, struct drm_i915_gem_object *obj_priv; obj = drm_gem_object_lookup(dev, file_priv, args->handle); - if (obj == NULL) + if (obj == NULL) { + DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n", + args->handle); return -EINVAL; + } obj_priv = obj->driver_private; obj_priv->pin_count--; @@ -392,5 +415,7 @@ int i915_gem_init_object(struct drm_device *dev, struct drm_gem_object *obj) void i915_gem_free_object(struct drm_device *dev, struct drm_gem_object *obj) { + i915_gem_object_unbind(dev, obj); + drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); } -- cgit v1.2.3 From abc896638fdcd8ccb457ad7b43dbe7ad229ba501 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 1 May 2008 20:12:39 -0700 Subject: Use krefs for refcounting. krefs are way easier than a custom-coded spinlock+int combo. --- linux-core/drmP.h | 12 +++++++----- linux-core/drm_gem.c | 31 +++++++++++++++++-------------- 2 files changed, 24 insertions(+), 19 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 2ed17b81..0df9f19b 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -54,6 +54,7 @@ #include /* For (un)lock_kernel */ #include #include +#include #include #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) #include @@ -614,20 +615,21 @@ struct drm_ati_pcigart_info { * DRM for its buffer objects. */ struct drm_gem_object { + /** Reference count of this object */ + struct kref refcount; + + /** Related drm device */ + struct drm_device *dev; + /** File representing the shmem storage */ struct file *filp; - spinlock_t lock; - /** * Size of the object, in bytes. Immutable over the object's * lifetime. */ size_t size; - /** Reference count of this object, protected by object_lock */ - int refcount; - void *driver_private; }; diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index ee3dbe47..d39585e9 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -73,13 +73,14 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) obj = kcalloc(1, sizeof(*obj), GFP_KERNEL); + obj->dev = dev; obj->filp = shmem_file_setup("drm mm object", size, 0); if (IS_ERR(obj->filp)) { kfree(obj); return NULL; } - obj->refcount = 1; + kref_init (&obj->refcount); obj->size = size; if (dev->driver->gem_init_object != NULL && @@ -352,27 +353,29 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private) void drm_gem_object_reference(struct drm_device *dev, struct drm_gem_object *obj) { - spin_lock(&obj->lock); - obj->refcount++; - spin_unlock(&obj->lock); + kref_get(&obj->refcount); } EXPORT_SYMBOL(drm_gem_object_reference); +static void +drm_gem_object_free (struct kref *kref) +{ + struct drm_gem_object *obj = (struct drm_gem_object *) kref; + struct drm_device *dev = obj->dev; + + if (dev->driver->gem_free_object != NULL) + dev->driver->gem_free_object(dev, obj); + + fput(obj->filp); + kfree(obj); +} + void drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj) { if (obj == NULL) return; - spin_lock(&obj->lock); - obj->refcount--; - spin_unlock(&obj->lock); - if (obj->refcount == 0) { - if (dev->driver->gem_free_object != NULL) - dev->driver->gem_free_object(dev, obj); - - fput(obj->filp); - kfree(obj); - } + kref_put (&obj->refcount, drm_gem_object_free); } EXPORT_SYMBOL(drm_gem_object_unreference); -- cgit v1.2.3 From 30efad5113944681c1abd6452e10355c105e9c39 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 1 May 2008 20:31:16 -0700 Subject: Fix gem ioctls to be 32/64-bit clean. mixed 32/64 bit systems need 'special' help for ioctl where the user-space and kernel-space datatypes differ. Fixing the datatypes to be the same size, and align the same way for both 32 and 64-bit ppc and x86 environments will elimiante the need to have magic 32/64-bit ioctl translation code. --- linux-core/drm_gem.c | 6 +++--- linux-core/i915_gem.c | 14 ++++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index d39585e9..41976bc7 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -233,7 +233,7 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, offset = args->offset; - read = obj->filp->f_op->read(obj->filp, (char __user *)args->data, + read = obj->filp->f_op->read(obj->filp, (char __user *)(uintptr_t)args->data_ptr, args->size, &offset); if (read != args->size) { drm_gem_object_unreference(dev, obj); @@ -270,7 +270,7 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, offset = args->offset; down_write(¤t->mm->mmap_sem); - args->addr = (void *)do_mmap(obj->filp, 0, args->size, + args->addr_ptr = (uint64_t) do_mmap(obj->filp, 0, args->size, PROT_READ | PROT_WRITE, MAP_SHARED, args->offset); up_write(¤t->mm->mmap_sem); @@ -300,7 +300,7 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, offset = args->offset; - written = obj->filp->f_op->write(obj->filp, (char __user *)args->data, + written = obj->filp->f_op->write(obj->filp, (char __user *)args->data_ptr, args->size, &offset); if (written != args->size) { drm_gem_object_unreference(dev, obj); diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 29b2d894..335f0618 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -167,6 +167,7 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, struct drm_gem_object *obj) { struct drm_i915_gem_relocation_entry reloc; + struct drm_i915_gem_relocation_entry __user *relocs; struct drm_i915_gem_object *obj_priv = obj->driver_private; int i; @@ -177,6 +178,7 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, return -ENOMEM; } + relocs = (struct drm_i915_gem_relocation_entry __user *) (uintptr_t) entry->relocs_ptr; /* Apply the relocations, using the GTT aperture to avoid cache * flushing requirements. */ @@ -187,7 +189,7 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, uint32_t reloc_val, *reloc_entry; int ret; - ret = copy_from_user(&reloc, entry->relocs + i, sizeof(reloc)); + ret = copy_from_user(&reloc, relocs + i, sizeof(reloc)); if (ret != 0) return ret; @@ -229,7 +231,7 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, reloc_val = target_obj_priv->gtt_offset + reloc.delta; DRM_DEBUG("Applied relocation: %p@0x%08x = 0x%08x\n", - obj, reloc.offset, reloc_val); + obj, (unsigned int) reloc.offset, reloc_val); *reloc_entry = reloc_val; iounmap(reloc_page); @@ -299,8 +301,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } ret = copy_from_user(validate_list, - (struct drm_i915_relocation_entry __user*) - args->buffers, + (struct drm_i915_relocation_entry __user*)(uintptr_t) + args->buffers_ptr, sizeof(*validate_list) * args->buffer_count); if (ret != 0) goto err; @@ -329,8 +331,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, validate_list[i].buffer_offset = obj_priv->gtt_offset; } ret = copy_to_user(validate_list, - (struct drm_i915_relocation_entry __user*) - args->buffers, + (struct drm_i915_relocation_entry __user*)(uintptr_t) + args->buffers_ptr, sizeof(*validate_list) * args->buffer_count); /* Clean up and return */ -- cgit v1.2.3 From 0d547c9ed92c0183f2c727496154baa2849f326e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 1 May 2008 20:41:55 -0700 Subject: Add alignment to all aperture allocation requests. When pinning buffers, or using execbuffer, allow the application to specify the necessary aperture allocation alignment constraints. --- linux-core/i915_gem.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 335f0618..30e67742 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -96,21 +96,26 @@ i915_gem_object_unbind(struct drm_device *dev, struct drm_gem_object *obj) * Finds free space in the GTT aperture and binds the object there. */ static int -i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj) +i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj, unsigned alignment) { drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; struct drm_memrange_node *free_space; int page_count, i; + if (alignment == 0) + alignment = PAGE_SIZE; + if (alignment & (PAGE_SIZE - 1)) + return -EINVAL; + free_space = drm_memrange_search_free(&dev_priv->mm.gtt_space, obj->size, - PAGE_SIZE, 0); + alignment, 0); if (free_space == NULL) return -ENOMEM; obj_priv->gtt_space = drm_memrange_get_block(free_space, obj->size, - PAGE_SIZE); + alignment); if (obj_priv->gtt_space == NULL) return -ENOMEM; @@ -173,7 +178,7 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, /* Choose the GTT offset for our buffer and put it there. */ if (obj_priv->gtt_space == NULL) { - i915_gem_object_bind_to_gtt(dev, obj); + i915_gem_object_bind_to_gtt(dev, obj, (unsigned) entry->alignment); if (obj_priv->gtt_space == NULL) return -ENOMEM; } @@ -367,7 +372,7 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, return -EINVAL; } - ret = i915_gem_object_bind_to_gtt(dev, obj); + ret = i915_gem_object_bind_to_gtt(dev, obj, (unsigned) args->alignment); if (ret != 0) { DRM_ERROR("Failure to bind in i915_gem_pin_ioctl(): %d\n", ret); -- cgit v1.2.3 From 49e8e3372afcf5fab9ffef5691d87ad8bc19599a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 2 May 2008 10:36:00 -0700 Subject: Remove drm_driver argument to functions taking drm_gem_object. Now that drm_gem_object has a drm_driver * in it, functions don't need both parameters. --- linux-core/drmP.h | 26 ++++++++++++++++++-------- linux-core/drm_gem.c | 45 ++++++++++++++------------------------------- linux-core/i915_gem.c | 44 +++++++++++++++++++++----------------------- 3 files changed, 53 insertions(+), 62 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 0df9f19b..d2dc065a 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -740,10 +740,8 @@ struct drm_driver { * * Returns 0 on success. */ - int (*gem_init_object) (struct drm_device *dev, - struct drm_gem_object *obj); - void (*gem_free_object) (struct drm_device *dev, - struct drm_gem_object *obj); + int (*gem_init_object) (struct drm_gem_object *obj); + void (*gem_free_object) (struct drm_gem_object *obj); struct drm_fence_driver *fence_driver; struct drm_bo_driver *bo_driver; @@ -1307,11 +1305,23 @@ static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) return block->mm; } +void +drm_gem_object_free (struct kref *kref); + /* Graphics Execution Manager library functions (drm_gem.c) */ -void drm_gem_object_reference(struct drm_device *dev, - struct drm_gem_object *obj); -void drm_gem_object_unreference(struct drm_device *dev, - struct drm_gem_object *obj); +static inline void drm_gem_object_reference(struct drm_gem_object *obj) +{ + kref_get(&obj->refcount); +} + +static inline void drm_gem_object_unreference(struct drm_gem_object *obj) +{ + if (obj == NULL) + return; + + kref_put (&obj->refcount, drm_gem_object_free); +} + struct drm_gem_object * drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, int handle); diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 41976bc7..80e8657b 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -84,7 +84,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) obj->size = size; if (dev->driver->gem_init_object != NULL && - dev->driver->gem_init_object(dev, obj) != 0) { + dev->driver->gem_init_object(obj) != 0) { fput(obj->filp); kfree(obj); return NULL; @@ -121,7 +121,7 @@ drm_gem_handle_delete(struct drm_device *dev, struct drm_file *filp, /* Release reference and decrement refcount. */ idr_remove(&filp->object_idr, handle); - drm_gem_object_unreference(dev, obj); + drm_gem_object_unreference(obj); spin_unlock(&filp->table_lock); @@ -144,7 +144,7 @@ drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, return NULL; } - drm_gem_object_reference(dev, obj); + drm_gem_object_reference(obj); spin_unlock(&filp->table_lock); @@ -189,7 +189,7 @@ drm_gem_alloc_ioctl(struct drm_device *dev, void *data, } while (ret == -EAGAIN); if (ret != 0) { - drm_gem_object_unreference(dev, obj); + drm_gem_object_unreference(obj); return -EFAULT; } @@ -236,14 +236,14 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, read = obj->filp->f_op->read(obj->filp, (char __user *)(uintptr_t)args->data_ptr, args->size, &offset); if (read != args->size) { - drm_gem_object_unreference(dev, obj); + drm_gem_object_unreference(obj); if (read < 0) return read; else return -EINVAL; } - drm_gem_object_unreference(dev, obj); + drm_gem_object_unreference(obj); return 0; } @@ -275,7 +275,7 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, args->offset); up_write(¤t->mm->mmap_sem); - drm_gem_object_unreference(dev, obj); + drm_gem_object_unreference(obj); return 0; } @@ -300,17 +300,17 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, offset = args->offset; - written = obj->filp->f_op->write(obj->filp, (char __user *)args->data_ptr, + written = obj->filp->f_op->write(obj->filp, (char __user *)(uintptr_t) args->data_ptr, args->size, &offset); if (written != args->size) { - drm_gem_object_unreference(dev, obj); + drm_gem_object_unreference(obj); if (written < 0) return written; else return -EINVAL; } - drm_gem_object_unreference(dev, obj); + drm_gem_object_unreference(obj); return 0; } @@ -329,10 +329,9 @@ drm_gem_open(struct drm_device *dev, struct drm_file *file_private) static int drm_gem_object_release(int id, void *ptr, void *data) { - struct drm_device *dev = data; struct drm_gem_object *obj = ptr; - drm_gem_object_unreference(dev, obj); + drm_gem_object_unreference(obj); return 0; } @@ -345,37 +344,21 @@ drm_gem_object_release(int id, void *ptr, void *data) void drm_gem_release(struct drm_device *dev, struct drm_file *file_private) { - idr_for_each(&file_private->object_idr, &drm_gem_object_release, dev); + idr_for_each(&file_private->object_idr, &drm_gem_object_release, NULL); idr_destroy(&file_private->object_idr); } void -drm_gem_object_reference(struct drm_device *dev, struct drm_gem_object *obj) -{ - kref_get(&obj->refcount); -} -EXPORT_SYMBOL(drm_gem_object_reference); - -static void drm_gem_object_free (struct kref *kref) { struct drm_gem_object *obj = (struct drm_gem_object *) kref; struct drm_device *dev = obj->dev; if (dev->driver->gem_free_object != NULL) - dev->driver->gem_free_object(dev, obj); + dev->driver->gem_free_object(obj); fput(obj->filp); kfree(obj); } - -void -drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj) -{ - if (obj == NULL) - return; - - kref_put (&obj->refcount, drm_gem_object_free); -} -EXPORT_SYMBOL(drm_gem_object_unreference); +EXPORT_SYMBOL(drm_gem_object_free); diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 30e67742..16bb3923 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -49,8 +49,7 @@ i915_gem_init_ioctl(struct drm_device *dev, void *data, } static void -i915_gem_object_free_page_list(struct drm_device *dev, - struct drm_gem_object *obj) +i915_gem_object_free_page_list(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; int page_count = obj->size / PAGE_SIZE; @@ -74,7 +73,7 @@ i915_gem_object_free_page_list(struct drm_device *dev, * Unbinds an object from the GTT aperture. */ static void -i915_gem_object_unbind(struct drm_device *dev, struct drm_gem_object *obj) +i915_gem_object_unbind(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -86,7 +85,7 @@ i915_gem_object_unbind(struct drm_device *dev, struct drm_gem_object *obj) drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); } - i915_gem_object_free_page_list(dev, obj); + i915_gem_object_free_page_list(obj); drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; @@ -96,8 +95,9 @@ i915_gem_object_unbind(struct drm_device *dev, struct drm_gem_object *obj) * Finds free space in the GTT aperture and binds the object there. */ static int -i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj, unsigned alignment) +i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) { + struct drm_device *dev = obj->dev; drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; struct drm_memrange_node *free_space; @@ -141,7 +141,7 @@ i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj, find_or_create_page(obj->filp->f_mapping, i, GFP_HIGHUSER); if (obj_priv->page_list[i] == NULL) { - i915_gem_object_free_page_list(dev, obj); + i915_gem_object_free_page_list(obj); drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; return -ENOMEM; @@ -156,7 +156,7 @@ i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj, page_count, obj_priv->gtt_offset); if (obj_priv->agp_mem == NULL) { - i915_gem_object_free_page_list(dev, obj); + i915_gem_object_free_page_list(obj); drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; return -ENOMEM; @@ -166,11 +166,11 @@ i915_gem_object_bind_to_gtt(struct drm_device *dev, struct drm_gem_object *obj, } static int -i915_gem_reloc_and_validate_object(struct drm_device *dev, +i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, struct drm_file *file_priv, - struct drm_i915_gem_validate_entry *entry, - struct drm_gem_object *obj) + struct drm_i915_gem_validate_entry *entry) { + struct drm_device *dev = obj->dev; struct drm_i915_gem_relocation_entry reloc; struct drm_i915_gem_relocation_entry __user *relocs; struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -178,7 +178,7 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, /* Choose the GTT offset for our buffer and put it there. */ if (obj_priv->gtt_space == NULL) { - i915_gem_object_bind_to_gtt(dev, obj, (unsigned) entry->alignment); + i915_gem_object_bind_to_gtt(obj, (unsigned) entry->alignment); if (obj_priv->gtt_space == NULL) return -ENOMEM; } @@ -198,7 +198,7 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, if (ret != 0) return ret; - target_obj = drm_gem_object_lookup(dev, file_priv, + target_obj = drm_gem_object_lookup(obj->dev, file_priv, reloc.target_handle); if (target_obj == NULL) return -EINVAL; @@ -248,12 +248,11 @@ i915_gem_reloc_and_validate_object(struct drm_device *dev, static int evict_callback(struct drm_memrange_node *node, void *data) { - struct drm_device *dev = data; struct drm_gem_object *obj = node->private; struct drm_i915_gem_object *obj_priv = obj->driver_private; if (obj_priv->pin_count == 0) - i915_gem_object_unbind(dev, obj); + i915_gem_object_unbind(obj); return 0; } @@ -274,7 +273,7 @@ i915_gem_sync_and_evict(struct drm_device *dev) return ret; /* Evict everything so we have space for sure. */ - drm_memrange_for_each(&dev_priv->mm.gtt_space, evict_callback, dev); + drm_memrange_for_each(&dev_priv->mm.gtt_space, evict_callback, NULL); return 0; } @@ -321,9 +320,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } - i915_gem_reloc_and_validate_object(dev, file_priv, - &validate_list[i], - object_list[i]); + i915_gem_reloc_and_validate_object(object_list[i], file_priv, + &validate_list[i]); } /* Exec the batchbuffer */ @@ -346,7 +344,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, err: if (object_list != NULL) { for (i = 0; i < args->buffer_count; i++) - drm_gem_object_unreference(dev, object_list[i]); + drm_gem_object_unreference(object_list[i]); } drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); @@ -372,7 +370,7 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, return -EINVAL; } - ret = i915_gem_object_bind_to_gtt(dev, obj, (unsigned) args->alignment); + ret = i915_gem_object_bind_to_gtt(obj, (unsigned) args->alignment); if (ret != 0) { DRM_ERROR("Failure to bind in i915_gem_pin_ioctl(): %d\n", ret); @@ -407,7 +405,7 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, return 0; } -int i915_gem_init_object(struct drm_device *dev, struct drm_gem_object *obj) +int i915_gem_init_object(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv; @@ -420,9 +418,9 @@ int i915_gem_init_object(struct drm_device *dev, struct drm_gem_object *obj) return 0; } -void i915_gem_free_object(struct drm_device *dev, struct drm_gem_object *obj) +void i915_gem_free_object(struct drm_gem_object *obj) { - i915_gem_object_unbind(dev, obj); + i915_gem_object_unbind(obj); drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); } -- cgit v1.2.3 From 39e20bcd5f4bf9fedac80188fda2e9fcab2f0360 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 2 May 2008 12:28:49 -0700 Subject: Add name/open ioctls, separate handle and pointer ref counts. Names are just another unique integer set (from another idr object). Names are removed when the user refernces (handles) are all destroyed -- this required that handles for objects be counted separately from internal kernel references (so that we can tell when the handles are all gone). --- linux-core/drmP.h | 43 +++++++++++ linux-core/drm_drv.c | 2 + linux-core/drm_gem.c | 193 ++++++++++++++++++++++++++++++++++++++++++-------- linux-core/i915_gem.c | 4 +- 4 files changed, 212 insertions(+), 30 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index d2dc065a..ebbbfa8a 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -618,6 +618,9 @@ struct drm_gem_object { /** Reference count of this object */ struct kref refcount; + /** Handle count of this object. Each handle also holds a reference */ + struct kref handlecount; + /** Related drm device */ struct drm_device *dev; @@ -630,6 +633,12 @@ struct drm_gem_object { */ size_t size; + /** + * Global name for this object, starts at 1. 0 means unnamed. + * Access is covered by the object_name_lock in the related drm_device + */ + int name; + void *driver_private; }; @@ -923,6 +932,12 @@ struct drm_device { spinlock_t drw_lock; struct idr drw_idr; /*@} */ + + /** \name GEM information */ + /*@{ */ + spinlock_t object_name_lock; + struct idr object_name_idr; + /*@} */ }; #if __OS_HAS_AGP @@ -1307,6 +1322,9 @@ static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) void drm_gem_object_free (struct kref *kref); + +void +drm_gem_object_handle_free (struct kref *kref); /* Graphics Execution Manager library functions (drm_gem.c) */ static inline void drm_gem_object_reference(struct drm_gem_object *obj) @@ -1322,6 +1340,26 @@ static inline void drm_gem_object_unreference(struct drm_gem_object *obj) kref_put (&obj->refcount, drm_gem_object_free); } +static inline void drm_gem_object_handle_reference (struct drm_gem_object *obj) +{ + drm_gem_object_reference (obj); + kref_get(&obj->handlecount); +} + +static inline void drm_gem_object_handle_unreference (struct drm_gem_object *obj) +{ + if (obj == NULL) + return; + + /* + * Must bump handle count first as this may be the last + * ref, in which case the object would disappear before we + * checked for a name + */ + kref_put (&obj->handlecount, drm_gem_object_handle_free); + drm_gem_object_unreference (obj); +} + struct drm_gem_object * drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, int handle); @@ -1335,6 +1373,11 @@ int drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int drm_gem_mmap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); +int drm_gem_name_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_gem_open_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); + void drm_gem_open(struct drm_device *dev, struct drm_file *file_private); void drm_gem_release(struct drm_device *dev, struct drm_file *file_private); diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index 11fedad6..4c85cdf4 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -156,6 +156,8 @@ static struct drm_ioctl_desc drm_ioctls[] = { DRM_IOCTL_DEF(DRM_IOCTL_GEM_PREAD, drm_gem_pread_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_GEM_PWRITE, drm_gem_pwrite_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_GEM_MMAP, drm_gem_mmap_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_NAME, drm_gem_name_ioctl, DRM_AUTH), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH), }; #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls ) diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 80e8657b..2e963f20 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -81,6 +81,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) } kref_init (&obj->refcount); + kref_init (&obj->handlecount); obj->size = size; if (dev->driver->gem_init_object != NULL && @@ -96,8 +97,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) * Removes the mapping from handle to filp for this object. */ static int -drm_gem_handle_delete(struct drm_device *dev, struct drm_file *filp, - int handle) +drm_gem_handle_delete(struct drm_file *filp, int handle) { struct drm_gem_object *obj; @@ -121,10 +121,45 @@ drm_gem_handle_delete(struct drm_device *dev, struct drm_file *filp, /* Release reference and decrement refcount. */ idr_remove(&filp->object_idr, handle); - drm_gem_object_unreference(obj); - spin_unlock(&filp->table_lock); + drm_gem_object_handle_unreference(obj); + + return 0; +} + +/** + * Create a handle for this object. This adds a handle reference + * to the object, which includes a regular reference count. Callers + * will likely want to dereference the object afterwards. + */ +static int +drm_gem_handle_create (struct drm_file *file_priv, + struct drm_gem_object *obj, + int *handlep) +{ + int ret; + + /* + * Get the user-visible handle using idr. + */ +again: + /* ensure there is space available to allocate a handle */ + if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) { + kfree(obj); + return -ENOMEM; + } + /* do the allocation under our spinlock */ + spin_lock (&file_priv->table_lock); + ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep); + spin_unlock (&file_priv->table_lock); + if (ret == -EAGAIN) + goto again; + + if (ret != 0) + return ret; + + drm_gem_object_handle_reference (obj); return 0; } @@ -171,27 +206,11 @@ drm_gem_alloc_ioctl(struct drm_device *dev, void *data, if (obj == NULL) return -ENOMEM; - /* Get the user-visible handle using idr. - * - * I'm not really sure why the idr api needs us to do this in two - * repeating steps. It handles internal locking of its data - * structure, yet insists that we keep its memory allocation step - * separate from its slot-finding step for locking purposes. - */ - do { - if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) { - kfree(obj); - return -EFAULT; - } + ret = drm_gem_handle_create (file_priv, obj, &handle); + drm_gem_object_handle_unreference(obj); - ret = idr_get_new_above(&file_priv->object_idr, obj, 1, - &handle); - } while (ret == -EAGAIN); - - if (ret != 0) { - drm_gem_object_unreference(obj); - return -EFAULT; - } + if (ret) + return ret; args->handle = handle; @@ -208,7 +227,7 @@ drm_gem_unreference_ioctl(struct drm_device *dev, void *data, struct drm_gem_unreference *args = data; int ret; - ret = drm_gem_handle_delete(dev, file_priv, args->handle); + ret = drm_gem_handle_delete(file_priv, args->handle); return ret; } @@ -315,6 +334,87 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, return 0; } +/** + * Create a global name for an object, returning the name. + * + * Note that the name does not hold a reference; when the object + * is freed, the name goes away. + */ +int +drm_gem_name_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_gem_name *args = data; + struct drm_gem_object *obj; + int ret; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + +again: + if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) { + return -ENOMEM; + } + spin_lock(&dev->object_name_lock); + if (obj->name) { + spin_unlock (&dev->object_name_lock); + return -EEXIST; + } + ret = idr_get_new_above (&dev->object_name_idr, obj, 1, + &obj->name); + spin_unlock (&dev->object_name_lock); + if (ret == -EAGAIN) + goto again; + + if (ret != 0) { + drm_gem_object_unreference(obj); + return ret; + } + + /* + * Leave the reference from the lookup around as the + * name table now holds one + */ + args->name = (uint64_t) obj->name; + + return 0; +} + +/** + * Open an object using the global name, returning a handle and the size. + * + * This handle (of course) holds a reference to the object, so the object + * will not go away until the handle is deleted. + */ +int +drm_gem_open_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_gem_open *args = data; + struct drm_gem_object *obj; + int ret; + int handle; + + spin_lock (&dev->object_name_lock); + obj = idr_find (&dev->object_name_idr, (int) args->name); + if (obj) + drm_gem_object_reference (obj); + spin_unlock (&dev->object_name_lock); + if (!obj) + return -ENOENT; + + ret = drm_gem_handle_create (file_priv, obj, &handle); + drm_gem_object_unreference (obj); + if (ret) + return ret; + + args->handle = handle; + args->size = obj->size; + + return 0; +} + /** * Called at device open time, sets up the structure for handling refcounting * of mm objects. @@ -325,13 +425,13 @@ drm_gem_open(struct drm_device *dev, struct drm_file *file_private) idr_init(&file_private->object_idr); } -/** Called at device close to release the file's references on objects. */ +/** Called at device close to release the file's handle references on objects. */ static int -drm_gem_object_release(int id, void *ptr, void *data) +drm_gem_object_release_handle (int id, void *ptr, void *data) { struct drm_gem_object *obj = ptr; - drm_gem_object_unreference(obj); + drm_gem_object_handle_unreference(obj); return 0; } @@ -344,11 +444,16 @@ drm_gem_object_release(int id, void *ptr, void *data) void drm_gem_release(struct drm_device *dev, struct drm_file *file_private) { - idr_for_each(&file_private->object_idr, &drm_gem_object_release, NULL); + idr_for_each(&file_private->object_idr, &drm_gem_object_release_handle, NULL); idr_destroy(&file_private->object_idr); } +/** + * Called after the last reference to the object has been lost. + * + * Frees the object + */ void drm_gem_object_free (struct kref *kref) { @@ -359,6 +464,36 @@ drm_gem_object_free (struct kref *kref) dev->driver->gem_free_object(obj); fput(obj->filp); + kfree(obj); } EXPORT_SYMBOL(drm_gem_object_free); + +/** + * Called after the last handle to the object has been closed + * + * Removes any name for the object. Note that this must be + * called before drm_gem_object_free or we'll be touching + * freed memory + */ +void +drm_gem_object_handle_free (struct kref *kref) +{ + struct drm_gem_object *obj = container_of (kref, struct drm_gem_object, handlecount); + struct drm_device *dev = obj->dev; + + /* Remove any name for this object */ + spin_lock (&dev->object_name_lock); + if (obj->name) { + idr_remove (&dev->object_name_idr, obj->name); + spin_unlock (&dev->object_name_lock); + /* + * The object name held a reference to this object, drop + * that now. + */ + drm_gem_object_unreference (obj); + } else + spin_unlock (&dev->object_name_lock); + +} +EXPORT_SYMBOL(drm_gem_object_handle_free); diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 16bb3923..2ac74b4b 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -105,8 +105,10 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) if (alignment == 0) alignment = PAGE_SIZE; - if (alignment & (PAGE_SIZE - 1)) + if (alignment & (PAGE_SIZE - 1)) { + DRM_ERROR("Invalid object alignment requested %u\n", alignment); return -EINVAL; + } free_space = drm_memrange_search_free(&dev_priv->mm.gtt_space, obj->size, -- cgit v1.2.3 From ab3549d1336fc6c08581a9fd14a83513649d9187 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 2 May 2008 16:34:16 -0700 Subject: Add a bit of /proc/dri/*/gem support. Clean up some refcount/pagelock issues. Track named objects in /proc/dri/0/gem_names. Track total object count in /proc/dri/0/gem_objects. Initialize device gem data. return -ENODEV for gem ioctls if the driver doesn't support gem. Call unlock_page when unbinding from gtt. Add numerous misssing calls to drm_gem_object_unreference. --- linux-core/drmP.h | 4 +++ linux-core/drm_gem.c | 46 +++++++++++++++++++++++++++--- linux-core/drm_proc.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ linux-core/drm_stub.c | 9 ++++++ linux-core/i915_drv.c | 2 +- linux-core/i915_gem.c | 36 ++++++++++++++++------- 6 files changed, 160 insertions(+), 16 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index ebbbfa8a..12e093e3 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -937,6 +937,7 @@ struct drm_device { /*@{ */ spinlock_t object_name_lock; struct idr object_name_idr; + atomic_t object_count; /*@} */ }; @@ -1320,6 +1321,9 @@ static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) return block->mm; } +int +drm_gem_init (struct drm_device *dev); + void drm_gem_object_free (struct kref *kref); diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 2e963f20..db12f9a6 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -64,6 +64,22 @@ * up at a later data, and as our interface with shmfs for memory allocation. */ +/** + * Initialize the GEM device fields + */ + +int +drm_gem_init (struct drm_device *dev) +{ + spin_lock_init (&dev->object_name_lock); + idr_init (&dev->object_name_idr); + atomic_set (&dev->object_count, 0); + return 0; +} + +/** + * Allocate a GEM object of the specified size with shmfs backing store + */ static struct drm_gem_object * drm_gem_object_alloc(struct drm_device *dev, size_t size) { @@ -90,6 +106,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) kfree(obj); return NULL; } + atomic_inc (&dev->object_count); return obj; } @@ -145,10 +162,9 @@ drm_gem_handle_create (struct drm_file *file_priv, */ again: /* ensure there is space available to allocate a handle */ - if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) { - kfree(obj); + if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) return -ENOMEM; - } + /* do the allocation under our spinlock */ spin_lock (&file_priv->table_lock); ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep); @@ -198,6 +214,9 @@ drm_gem_alloc_ioctl(struct drm_device *dev, void *data, struct drm_gem_object *obj; int handle, ret; + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + /* Round requested size up to page size */ args->size = (args->size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); @@ -227,6 +246,9 @@ drm_gem_unreference_ioctl(struct drm_device *dev, void *data, struct drm_gem_unreference *args = data; int ret; + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + ret = drm_gem_handle_delete(file_priv, args->handle); return ret; @@ -246,6 +268,9 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, ssize_t read; loff_t offset; + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) return -EINVAL; @@ -282,6 +307,9 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, struct drm_gem_object *obj; loff_t offset; + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) return -EINVAL; @@ -313,6 +341,9 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, ssize_t written; loff_t offset; + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) return -EINVAL; @@ -348,6 +379,9 @@ drm_gem_name_ioctl(struct drm_device *dev, void *data, struct drm_gem_object *obj; int ret; + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) return -EINVAL; @@ -396,6 +430,9 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data, int ret; int handle; + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + spin_lock (&dev->object_name_lock); obj = idr_find (&dev->object_name_idr, (int) args->name); if (obj) @@ -423,6 +460,7 @@ void drm_gem_open(struct drm_device *dev, struct drm_file *file_private) { idr_init(&file_private->object_idr); + spin_lock_init (&file_private->table_lock); } /** Called at device close to release the file's handle references on objects. */ @@ -464,7 +502,7 @@ drm_gem_object_free (struct kref *kref) dev->driver->gem_free_object(obj); fput(obj->filp); - + atomic_dec (&dev->object_count); kfree(obj); } EXPORT_SYMBOL(drm_gem_object_free); diff --git a/linux-core/drm_proc.c b/linux-core/drm_proc.c index 42da5c69..b1b976b2 100644 --- a/linux-core/drm_proc.c +++ b/linux-core/drm_proc.c @@ -51,6 +51,10 @@ static int drm_bufs_info(char *buf, char **start, off_t offset, int request, int *eof, void *data); static int drm_objects_info(char *buf, char **start, off_t offset, int request, int *eof, void *data); +static int drm_gem_name_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data); +static int drm_gem_object_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data); #if DRM_DEBUG_CODE static int drm_vma_info(char *buf, char **start, off_t offset, int request, int *eof, void *data); @@ -70,6 +74,8 @@ static struct drm_proc_list { {"queues", drm_queues_info}, {"bufs", drm_bufs_info}, {"objects", drm_objects_info}, + {"gem_names", drm_gem_name_info}, + {"gem_objects", drm_gem_object_info}, #if DRM_DEBUG_CODE {"vma", drm_vma_info}, #endif @@ -582,6 +588,79 @@ static int drm_clients_info(char *buf, char **start, off_t offset, return ret; } +struct drm_gem_name_info_data { + int len; + char *buf; + int eof; +}; + +static int drm_gem_one_name_info (int id, void *ptr, void *data) +{ + struct drm_gem_object *obj = ptr; + struct drm_gem_name_info_data *nid = data; + + DRM_INFO ("name %d size %d\n", obj->name, obj->size); + if (nid->eof) + return 0; + + nid->len += sprintf (&nid->buf[nid->len], + "%6d%9d%8d%9d\n", + obj->name, obj->size, + atomic_read(&obj->handlecount.refcount), + atomic_read(&obj->refcount.refcount)); + if (nid->len > DRM_PROC_LIMIT) { + nid->eof = 1; + return 0; + } + return 0; +} + +static int drm_gem_name_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + struct drm_gem_name_info_data nid; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + nid.len = sprintf (buf, " name size handles refcount\n"); + nid.buf = buf; + nid.eof = 0; + idr_for_each (&dev->object_name_idr, drm_gem_one_name_info, &nid); + + *start = &buf[offset]; + *eof = 0; + if (nid.len > request + offset) + return request; + *eof = 1; + return nid.len - offset; +} + +static int drm_gem_object_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT ("%d objects\n", atomic_read (&dev->object_count)); + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + #if DRM_DEBUG_CODE static int drm__vma_info(char *buf, char **start, off_t offset, int request, diff --git a/linux-core/drm_stub.c b/linux-core/drm_stub.c index 030fea54..55841826 100644 --- a/linux-core/drm_stub.c +++ b/linux-core/drm_stub.c @@ -163,7 +163,16 @@ static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev, goto error_out_unreg; } + if (driver->driver_features & DRIVER_GEM) { + retcode = drm_gem_init (dev); + if (retcode) { + DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n"); + goto error_out_unreg; + } + } + drm_fence_manager_init(dev); + return 0; error_out_unreg: diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index c77c329f..3e788d25 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -566,7 +566,7 @@ static struct drm_driver driver = { */ .driver_features = DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | /* DRIVER_USE_MTRR | */ - DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED, + DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM, .load = i915_driver_load, .unload = i915_driver_unload, .firstopen = i915_driver_firstopen, diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 2ac74b4b..747cc45c 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -58,11 +58,13 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) if (obj_priv->page_list == NULL) return; - for (i = 0; i < obj->size / PAGE_SIZE; i++) { - if (obj_priv->page_list[i] == NULL) - put_page(obj_priv->page_list[i]); + + for (i = 0; i < page_count; i++) { + if (obj_priv->page_list[i] != NULL) { + unlock_page (obj_priv->page_list[i]); + page_cache_release (obj_priv->page_list[i]); + } } - drm_free(obj_priv->page_list, page_count * sizeof(struct page *), DRM_MEM_DRIVER); @@ -212,15 +214,18 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, if (target_obj_priv->gtt_space == NULL) { DRM_ERROR("No GTT space found for object %d\n", reloc.target_handle); + drm_gem_object_unreference (target_obj); return -EINVAL; } if (reloc.offset > obj->size - 4) { DRM_ERROR("Relocation beyond object bounds.\n"); + drm_gem_object_unreference (target_obj); return -EINVAL; } if (reloc.offset & 3) { DRM_ERROR("Relocation not 4-byte aligned.\n"); + drm_gem_object_unreference (target_obj); return -EINVAL; } @@ -231,7 +236,10 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, (reloc.offset & ~(PAGE_SIZE - 1)), PAGE_SIZE); if (reloc_page == NULL) + { + drm_gem_object_unreference (target_obj); return -ENOMEM; + } reloc_entry = (uint32_t *)((char *)reloc_page + (reloc.offset & (PAGE_SIZE - 1))); @@ -242,6 +250,7 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, *reloc_entry = reloc_val; iounmap(reloc_page); + drm_gem_object_unreference (target_obj); } return 0; @@ -372,16 +381,21 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, return -EINVAL; } - ret = i915_gem_object_bind_to_gtt(obj, (unsigned) args->alignment); - if (ret != 0) { - DRM_ERROR("Failure to bind in i915_gem_pin_ioctl(): %d\n", - ret); - return ret; + obj_priv = obj->driver_private; + if (obj_priv->gtt_space == NULL) + { + ret = i915_gem_object_bind_to_gtt(obj, (unsigned) args->alignment); + if (ret != 0) { + DRM_ERROR("Failure to bind in i915_gem_pin_ioctl(): %d\n", + ret); + drm_gem_object_unreference (obj); + return ret; + } } - obj_priv = obj->driver_private; obj_priv->pin_count++; args->offset = obj_priv->gtt_offset; + drm_gem_object_unreference (obj); return 0; } @@ -403,7 +417,7 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, obj_priv = obj->driver_private; obj_priv->pin_count--; - + drm_gem_object_unreference (obj); return 0; } -- cgit v1.2.3 From 5f0614b86ff5760016bef87c6f6012fe4f42e14e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 2 May 2008 17:13:11 -0700 Subject: Check for do_mmap errors --- linux-core/drm_gem.c | 12 ++++++++---- linux-core/i915_gem.c | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index db12f9a6..80d5a350 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -306,6 +306,7 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, struct drm_gem_mmap *args = data; struct drm_gem_object *obj; loff_t offset; + unsigned long addr; if (!(dev->driver->driver_features & DRIVER_GEM)) return -ENODEV; @@ -317,12 +318,15 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, offset = args->offset; down_write(¤t->mm->mmap_sem); - args->addr_ptr = (uint64_t) do_mmap(obj->filp, 0, args->size, - PROT_READ | PROT_WRITE, MAP_SHARED, - args->offset); + addr = do_mmap(obj->filp, 0, args->size, + PROT_READ | PROT_WRITE, MAP_SHARED, + args->offset); up_write(¤t->mm->mmap_sem); - drm_gem_object_unreference(obj); + if (IS_ERR((void *)addr)) + return (int) addr; + + args->addr_ptr = (uint64_t) addr; return 0; } diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 747cc45c..e0beeb43 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -300,12 +300,14 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, LOCK_TEST_WITH_RETURN(dev, file_priv); + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Big hammer: flush and idle the hardware so we can map things in/out. */ ret = i915_gem_sync_and_evict(dev); if (ret != 0) return ret; + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Copy in the validate list from userland */ validate_list = drm_calloc(sizeof(*validate_list), args->buffer_count, DRM_MEM_DRIVER); @@ -315,6 +317,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, ret = -ENOMEM; goto err; } + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); ret = copy_from_user(validate_list, (struct drm_i915_relocation_entry __user*)(uintptr_t) args->buffers_ptr, @@ -322,6 +325,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, if (ret != 0) goto err; + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Look up object handles and perform the relocations */ for (i = 0; i < args->buffer_count; i++) { object_list[i] = drm_gem_object_lookup(dev, file_priv, @@ -337,6 +341,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* Exec the batchbuffer */ + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Copy the new buffer offsets back to the user's validate list. */ for (i = 0; i < args->buffer_count; i++) { struct drm_i915_gem_object *obj_priv = @@ -344,24 +349,29 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, validate_list[i].buffer_offset = obj_priv->gtt_offset; } + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); ret = copy_to_user(validate_list, (struct drm_i915_relocation_entry __user*)(uintptr_t) args->buffers_ptr, sizeof(*validate_list) * args->buffer_count); + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Clean up and return */ ret = i915_gem_sync_and_evict(dev); + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); err: if (object_list != NULL) { for (i = 0; i < args->buffer_count; i++) drm_gem_object_unreference(object_list[i]); } + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, DRM_MEM_DRIVER); + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); return ret; } -- cgit v1.2.3 From afe574f328fca42f2fa5fbc1c7a1c13d0f35d2f6 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 2 May 2008 17:49:52 -0700 Subject: Don't include the tail guard memrange in foreach callbacking. --- linux-core/drm_memrange.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_memrange.c b/linux-core/drm_memrange.c index 663943ab..7304cbaa 100644 --- a/linux-core/drm_memrange.c +++ b/linux-core/drm_memrange.c @@ -291,10 +291,11 @@ int drm_memrange_for_each(struct drm_memrange *mm, int ret; cur = list_entry(list, struct drm_memrange_node, ml_entry); - - ret = callback(cur, data); - if (ret != 0) - return ret; + if (!cur->free) { + ret = callback(cur, data); + if (ret != 0) + return ret; + } } return 0; -- cgit v1.2.3 From 166ff364fb09ec9885a164fca517c079a1d88718 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 2 May 2008 17:50:46 -0700 Subject: Don't forget to set the memrange private, and reset ring on kernel entry. --- linux-core/i915_gem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index e0beeb43..8a30d846 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -122,7 +122,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) alignment); if (obj_priv->gtt_space == NULL) return -ENOMEM; - + obj_priv->gtt_space->private = obj; obj_priv->gtt_offset = obj_priv->gtt_space->start; DRM_DEBUG("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); @@ -300,6 +300,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, LOCK_TEST_WITH_RETURN(dev, file_priv); + i915_kernel_lost_context(dev); + DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Big hammer: flush and idle the hardware so we can map things in/out. */ -- cgit v1.2.3 From b6f173c4300e90be9bdd3b24003b800afd8819c5 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 5 May 2008 10:51:49 -0700 Subject: Add i915_dispatch_gem_execbuffer (broken). This function submits a gem-based execbuffer to the ring. It doesn't work yet. --- linux-core/drmP.h | 4 ++ linux-core/i915_gem.c | 115 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 100 insertions(+), 19 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 12e093e3..21a4dcd0 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -90,6 +90,10 @@ struct drm_device; struct drm_file; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) +typedef unsigned long uintptr_t; +#endif + /* If you want the memory alloc debug functionality, change define below */ /* #define DEBUG_MEMORY */ diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 8a30d846..95f6a267 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -79,18 +79,24 @@ i915_gem_object_unbind(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; + DRM_INFO ("%s:%d %p\n", __FUNCTION__, __LINE__, obj); + DRM_INFO ("gtt_space %p\n", obj_priv->gtt_space); if (obj_priv->gtt_space == NULL) return; + DRM_INFO ("agp_mem %p %ld pages\n", obj_priv->agp_mem, obj->size / PAGE_SIZE); if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); } + DRM_INFO ("free_page_list\n"); i915_gem_object_free_page_list(obj); + DRM_INFO ("put_block\n"); drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; + DRM_INFO ("done\n"); } /** @@ -259,9 +265,18 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, static int evict_callback(struct drm_memrange_node *node, void *data) { - struct drm_gem_object *obj = node->private; - struct drm_i915_gem_object *obj_priv = obj->driver_private; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + + DRM_INFO ("evict node %p\n", node); + + obj = node->private; + DRM_INFO ("evict obj %p\n", obj); + + obj_priv = obj->driver_private; + DRM_INFO ("evict priv %p\n", obj_priv); + DRM_INFO ("pin_count %d\n", obj_priv->pin_count); if (obj_priv->pin_count == 0) i915_gem_object_unbind(obj); @@ -289,6 +304,58 @@ i915_gem_sync_and_evict(struct drm_device *dev) return 0; } +static int +i915_dispatch_gem_execbuffer (struct drm_device * dev, + struct drm_i915_gem_execbuffer * exec) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *) (uintptr_t) exec->cliprects_ptr; + int nbox = exec->num_cliprects; + int i = 0, count; + RING_LOCALS; + + if ((exec->batch_start_offset | exec->batch_len) & 0x7) { + DRM_ERROR("alignment\n"); + return -EINVAL; + } + + i915_kernel_lost_context(dev); + + count = nbox ? nbox : 1; + + for (i = 0; i < count; i++) { + if (i < nbox) { + int ret = i915_emit_box(dev, boxes, i, + exec->DR1, exec->DR4); + if (ret) + return ret; + } + + if (dev_priv->use_mi_batchbuffer_start) { + BEGIN_LP_RING(2); + if (IS_I965G(dev)) { + OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965); + OUT_RING(exec->batch_start_offset); + } else { + OUT_RING(MI_BATCH_BUFFER_START | (2 << 6)); + OUT_RING(exec->batch_start_offset | MI_BATCH_NON_SECURE); + } + ADVANCE_LP_RING(); + + } else { + BEGIN_LP_RING(4); + OUT_RING(MI_BATCH_BUFFER); + OUT_RING(exec->batch_start_offset | MI_BATCH_NON_SECURE); + OUT_RING(exec->batch_start_offset + exec->batch_len - 4); + OUT_RING(0); + ADVANCE_LP_RING(); + } + } + + /* XXX breadcrumb */ + return 0; +} + int i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -300,39 +367,45 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, LOCK_TEST_WITH_RETURN(dev, file_priv); + DRM_INFO ("buffers_ptr %d buffer_count %d\n", + (int) args->buffers_ptr, args->buffer_count); i915_kernel_lost_context(dev); - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Big hammer: flush and idle the hardware so we can map things in/out. */ ret = i915_gem_sync_and_evict(dev); - if (ret != 0) + if (ret != 0) { + DRM_ERROR ("i915_gem_sync_and_evict failed %d\n", ret); return ret; + } - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Copy in the validate list from userland */ validate_list = drm_calloc(sizeof(*validate_list), args->buffer_count, DRM_MEM_DRIVER); object_list = drm_calloc(sizeof(*object_list), args->buffer_count, DRM_MEM_DRIVER); if (validate_list == NULL || object_list == NULL) { + DRM_ERROR ("Failed to allocate validate or object list for %d buffers\n", + args->buffer_count); ret = -ENOMEM; goto err; } - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); ret = copy_from_user(validate_list, (struct drm_i915_relocation_entry __user*)(uintptr_t) args->buffers_ptr, sizeof(*validate_list) * args->buffer_count); - if (ret != 0) + if (ret != 0) { + DRM_ERROR ("copy %d validate entries failed %d\n", args->buffer_count, ret); goto err; + } - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Look up object handles and perform the relocations */ for (i = 0; i < args->buffer_count; i++) { object_list[i] = drm_gem_object_lookup(dev, file_priv, validate_list[i].buffer_handle); if (object_list[i] == NULL) { + DRM_ERROR ("Invalid object handle %d at index %d\n", + validate_list[i].buffer_handle, i); ret = -EINVAL; goto err; } @@ -342,8 +415,13 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } /* Exec the batchbuffer */ + ret = i915_dispatch_gem_execbuffer (dev, args); + if (ret) + { + DRM_ERROR ("dispatch failed %d\n", ret); + goto err; + } - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Copy the new buffer offsets back to the user's validate list. */ for (i = 0; i < args->buffer_count; i++) { struct drm_i915_gem_object *obj_priv = @@ -351,29 +429,28 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, validate_list[i].buffer_offset = obj_priv->gtt_offset; } - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); - ret = copy_to_user(validate_list, - (struct drm_i915_relocation_entry __user*)(uintptr_t) - args->buffers_ptr, - sizeof(*validate_list) * args->buffer_count); + ret = copy_to_user((struct drm_i915_relocation_entry __user*)(uintptr_t) + args->buffers_ptr, + validate_list, + sizeof(*validate_list) * args->buffer_count); + if (ret) + DRM_ERROR ("failed to copy %d validate entries back to user (%d)\n", + args->buffer_count, ret); - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); /* Clean up and return */ ret = i915_gem_sync_and_evict(dev); - - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); + if (ret) + DRM_ERROR ("failed to sync/evict buffers %d\n", ret); err: if (object_list != NULL) { for (i = 0; i < args->buffer_count; i++) drm_gem_object_unreference(object_list[i]); } - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, DRM_MEM_DRIVER); - DRM_INFO ("%s:%d\n", __FUNCTION__, __LINE__); return ret; } -- cgit v1.2.3 From 4511e6cd80b4c47a142db48727753da8d0898857 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 5 May 2008 11:27:06 -0700 Subject: Correct execbuffer offset. Add memory barrier and chipset flush. --- linux-core/i915_gem.c | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 95f6a267..7e4e8d5f 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -192,6 +192,7 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, if (obj_priv->gtt_space == NULL) return -ENOMEM; } + entry->buffer_offset = obj_priv->gtt_offset; relocs = (struct drm_i915_gem_relocation_entry __user *) (uintptr_t) entry->relocs_ptr; /* Apply the relocations, using the GTT aperture to avoid cache @@ -306,21 +307,32 @@ i915_gem_sync_and_evict(struct drm_device *dev) static int i915_dispatch_gem_execbuffer (struct drm_device * dev, - struct drm_i915_gem_execbuffer * exec) + struct drm_i915_gem_execbuffer * exec, + uint64_t exec_offset) { drm_i915_private_t *dev_priv = dev->dev_private; struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *) (uintptr_t) exec->cliprects_ptr; int nbox = exec->num_cliprects; int i = 0, count; + uint32_t exec_start, exec_len; RING_LOCALS; - if ((exec->batch_start_offset | exec->batch_len) & 0x7) { + exec_start = (uint32_t) exec_offset + exec->batch_start_offset; + exec_len = (uint32_t) exec->batch_len; + + if ((exec_start | exec_len) & 0x7) { DRM_ERROR("alignment\n"); return -EINVAL; } i915_kernel_lost_context(dev); + DRM_INFO ("execbuffer at %x+%d len %d\n", + (uint32_t) exec_offset, exec->batch_start_offset, exec_len); + + if (!exec_start) + return -EINVAL; + count = nbox ? nbox : 1; for (i = 0; i < count; i++) { @@ -335,18 +347,18 @@ i915_dispatch_gem_execbuffer (struct drm_device * dev, BEGIN_LP_RING(2); if (IS_I965G(dev)) { OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965); - OUT_RING(exec->batch_start_offset); + OUT_RING(exec_start); } else { OUT_RING(MI_BATCH_BUFFER_START | (2 << 6)); - OUT_RING(exec->batch_start_offset | MI_BATCH_NON_SECURE); + OUT_RING(exec_start | MI_BATCH_NON_SECURE); } ADVANCE_LP_RING(); } else { BEGIN_LP_RING(4); OUT_RING(MI_BATCH_BUFFER); - OUT_RING(exec->batch_start_offset | MI_BATCH_NON_SECURE); - OUT_RING(exec->batch_start_offset + exec->batch_len - 4); + OUT_RING(exec_start | MI_BATCH_NON_SECURE); + OUT_RING(exec_start + exec_len - 4); OUT_RING(0); ADVANCE_LP_RING(); } @@ -364,6 +376,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_i915_gem_validate_entry *validate_list; struct drm_gem_object **object_list; int ret, i; + uint64_t exec_offset; LOCK_TEST_WITH_RETURN(dev, file_priv); @@ -410,12 +423,22 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } - i915_gem_reloc_and_validate_object(object_list[i], file_priv, - &validate_list[i]); + ret = i915_gem_reloc_and_validate_object(object_list[i], file_priv, + &validate_list[i]); + if (ret) { + DRM_ERROR ("reloc and validate failed %d\n", ret); + goto err; + } } + exec_offset = validate_list[args->buffer_count - 1].buffer_offset; + + /* make sure all previous memory operations have passed */ + DRM_MEMORYBARRIER(); + drm_agp_chipset_flush(dev); + /* Exec the batchbuffer */ - ret = i915_dispatch_gem_execbuffer (dev, args); + ret = i915_dispatch_gem_execbuffer (dev, args, exec_offset); if (ret) { DRM_ERROR ("dispatch failed %d\n", ret); @@ -423,12 +446,6 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } /* Copy the new buffer offsets back to the user's validate list. */ - for (i = 0; i < args->buffer_count; i++) { - struct drm_i915_gem_object *obj_priv = - object_list[i]->driver_private; - - validate_list[i].buffer_offset = obj_priv->gtt_offset; - } ret = copy_to_user((struct drm_i915_relocation_entry __user*)(uintptr_t) args->buffers_ptr, validate_list, -- cgit v1.2.3 From 4867780bd6900293880d1db963798d075ec9b01a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 5 May 2008 13:32:28 -0700 Subject: Emit clflush and chipset flush when mapping objects to gtt --- linux-core/Makefile | 1 + linux-core/i915_gem.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/Makefile b/linux-core/Makefile index 3af6f370..fc32676f 100644 --- a/linux-core/Makefile +++ b/linux-core/Makefile @@ -30,6 +30,7 @@ # # make DRM_MODULES="r128 radeon" # +DRM_MODULES=i915 SHELL=/bin/sh diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 7e4e8d5f..b66c7865 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -131,7 +131,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) obj_priv->gtt_space->private = obj; obj_priv->gtt_offset = obj_priv->gtt_space->start; - DRM_DEBUG("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); + DRM_INFO ("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); /* Get the list of pages out of our struct file. They'll be pinned * at this point until we release them. @@ -157,6 +157,10 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) return -ENOMEM; } } + + drm_ttm_cache_flush (obj_priv->page_list, page_count); + DRM_MEMORYBARRIER(); + drm_agp_chipset_flush(dev); /* Create an AGP memory structure pointing at our pages, and bind it * into the GTT. @@ -172,6 +176,20 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) return -ENOMEM; } + { + uint32_t *mem = kmap_atomic (obj_priv->page_list[0], KM_USER0); + volatile uint32_t *gtt = ioremap(dev->agp->base + obj_priv->gtt_offset, + PAGE_SIZE); + int i; + + DRM_INFO ("object at offset %08x agp base %08x gtt addr %p\n", + obj_priv->gtt_offset, (int) dev->agp->base, gtt); + for (i = 0; i < 16; i++) + DRM_INFO ("%3d: mem %08x gtt %08x\n", i, mem[i], gtt[i]); + iounmap (gtt); + kunmap_atomic (mem, KM_USER0); + } + return 0; } @@ -434,8 +452,6 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, exec_offset = validate_list[args->buffer_count - 1].buffer_offset; /* make sure all previous memory operations have passed */ - DRM_MEMORYBARRIER(); - drm_agp_chipset_flush(dev); /* Exec the batchbuffer */ ret = i915_dispatch_gem_execbuffer (dev, args, exec_offset); -- cgit v1.2.3 From f0bc796a028dc7c6281d3d0cb2deef9df37e380a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 5 May 2008 14:22:42 -0700 Subject: Add object base to relocation store address. The relocated value was being written to the wrong location, missing the object base address. --- linux-core/i915_gem.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index b66c7865..a1ce139b 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -99,6 +99,23 @@ i915_gem_object_unbind(struct drm_gem_object *obj) DRM_INFO ("done\n"); } +static void +i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where) +{ + struct drm_device *dev = obj->dev; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + uint32_t *mem = kmap_atomic (obj_priv->page_list[0], KM_USER0); + volatile uint32_t *gtt = ioremap(dev->agp->base + obj_priv->gtt_offset, + PAGE_SIZE); + int i; + + DRM_INFO ("%s: object at offset %08x\n", where, obj_priv->gtt_offset); + for (i = 0; i < len/4; i++) + DRM_INFO ("%3d: mem %08x gtt %08x\n", i, mem[i], readl(gtt + i)); + iounmap (gtt); + kunmap_atomic (mem, KM_USER0); +} + /** * Finds free space in the GTT aperture and binds the object there. */ @@ -176,20 +193,6 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) return -ENOMEM; } - { - uint32_t *mem = kmap_atomic (obj_priv->page_list[0], KM_USER0); - volatile uint32_t *gtt = ioremap(dev->agp->base + obj_priv->gtt_offset, - PAGE_SIZE); - int i; - - DRM_INFO ("object at offset %08x agp base %08x gtt addr %p\n", - obj_priv->gtt_offset, (int) dev->agp->base, gtt); - for (i = 0; i < 16; i++) - DRM_INFO ("%3d: mem %08x gtt %08x\n", i, mem[i], gtt[i]); - iounmap (gtt); - kunmap_atomic (mem, KM_USER0); - } - return 0; } @@ -220,7 +223,7 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, struct drm_gem_object *target_obj; struct drm_i915_gem_object *target_obj_priv; void *reloc_page; - uint32_t reloc_val, *reloc_entry; + uint32_t reloc_val, reloc_offset, *reloc_entry; int ret; ret = copy_from_user(&reloc, relocs + i, sizeof(reloc)); @@ -257,8 +260,9 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, /* Map the page containing the relocation we're going to * perform. */ + reloc_offset = obj_priv->gtt_offset + reloc.offset; reloc_page = ioremap(dev->agp->base + - (reloc.offset & ~(PAGE_SIZE - 1)), + (reloc_offset & ~(PAGE_SIZE - 1)), PAGE_SIZE); if (reloc_page == NULL) { @@ -267,17 +271,19 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, } reloc_entry = (uint32_t *)((char *)reloc_page + - (reloc.offset & (PAGE_SIZE - 1))); + (reloc_offset & (PAGE_SIZE - 1))); reloc_val = target_obj_priv->gtt_offset + reloc.delta; - DRM_DEBUG("Applied relocation: %p@0x%08x = 0x%08x\n", - obj, (unsigned int) reloc.offset, reloc_val); - *reloc_entry = reloc_val; + DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n", + obj, (unsigned int) reloc.offset, + readl (reloc_entry), reloc_val); + writel (reloc_val, reloc_entry); iounmap(reloc_page); drm_gem_object_unreference (target_obj); } + i915_gem_dump_object (obj, 128, __FUNCTION__); return 0; } -- cgit v1.2.3 From d59a9300ec2ec5d6dc606f847a7589c197994793 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 5 May 2008 14:32:01 -0700 Subject: Remove some debug messages. --- linux-core/i915_gem.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index a1ce139b..8d606f85 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -79,24 +79,22 @@ i915_gem_object_unbind(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; +#if 0 DRM_INFO ("%s:%d %p\n", __FUNCTION__, __LINE__, obj); DRM_INFO ("gtt_space %p\n", obj_priv->gtt_space); +#endif if (obj_priv->gtt_space == NULL) return; - DRM_INFO ("agp_mem %p %ld pages\n", obj_priv->agp_mem, obj->size / PAGE_SIZE); if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); } - DRM_INFO ("free_page_list\n"); i915_gem_object_free_page_list(obj); - DRM_INFO ("put_block\n"); drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; - DRM_INFO ("done\n"); } static void @@ -148,7 +146,9 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) obj_priv->gtt_space->private = obj; obj_priv->gtt_offset = obj_priv->gtt_space->start; +#if 0 DRM_INFO ("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); +#endif /* Get the list of pages out of our struct file. They'll be pinned * at this point until we release them. @@ -274,34 +274,27 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, (reloc_offset & (PAGE_SIZE - 1))); reloc_val = target_obj_priv->gtt_offset + reloc.delta; +#if 0 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n", obj, (unsigned int) reloc.offset, readl (reloc_entry), reloc_val); +#endif writel (reloc_val, reloc_entry); iounmap(reloc_page); drm_gem_object_unreference (target_obj); } - i915_gem_dump_object (obj, 128, __FUNCTION__); +/* i915_gem_dump_object (obj, 128, __FUNCTION__); */ return 0; } static int evict_callback(struct drm_memrange_node *node, void *data) { - struct drm_gem_object *obj; - struct drm_i915_gem_object *obj_priv; - - DRM_INFO ("evict node %p\n", node); - - obj = node->private; - DRM_INFO ("evict obj %p\n", obj); + struct drm_gem_object *obj = node->private; + struct drm_i915_gem_object *obj_priv = obj->driver_private; - obj_priv = obj->driver_private; - DRM_INFO ("evict priv %p\n", obj_priv); - - DRM_INFO ("pin_count %d\n", obj_priv->pin_count); if (obj_priv->pin_count == 0) i915_gem_object_unbind(obj); @@ -351,8 +344,10 @@ i915_dispatch_gem_execbuffer (struct drm_device * dev, i915_kernel_lost_context(dev); +#if 0 DRM_INFO ("execbuffer at %x+%d len %d\n", (uint32_t) exec_offset, exec->batch_start_offset, exec_len); +#endif if (!exec_start) return -EINVAL; @@ -404,8 +399,10 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, LOCK_TEST_WITH_RETURN(dev, file_priv); +#if 0 DRM_INFO ("buffers_ptr %d buffer_count %d\n", (int) args->buffers_ptr, args->buffer_count); +#endif i915_kernel_lost_context(dev); /* Big hammer: flush and idle the hardware so we can map things in/out. -- cgit v1.2.3 From dafe48e6239a4e9b49dd87b8c70224e8eeeb6079 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 5 May 2008 14:38:04 -0700 Subject: GEM: Replace drm_memrange_for_each with just evicting what we brought in. I was wrong about how the data structure worked, and didn't care to fix it to support debugging code. --- linux-core/drmP.h | 4 ---- linux-core/drm_memrange.c | 27 -------------------------- linux-core/i915_gem.c | 49 +++++++++++++++-------------------------------- 3 files changed, 15 insertions(+), 65 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 21a4dcd0..ffeafc18 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -1315,10 +1315,6 @@ extern int drm_memrange_remove_space_from_tail(struct drm_memrange *mm, unsigned long size); extern int drm_memrange_add_space_to_tail(struct drm_memrange *mm, unsigned long size); -extern int drm_memrange_for_each(struct drm_memrange *mm, - int (*callback)(struct drm_memrange_node *node, - void *data), - void *data); static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) { diff --git a/linux-core/drm_memrange.c b/linux-core/drm_memrange.c index 7304cbaa..7014c4e2 100644 --- a/linux-core/drm_memrange.c +++ b/linux-core/drm_memrange.c @@ -275,33 +275,6 @@ int drm_memrange_init(struct drm_memrange * mm, unsigned long start, unsigned lo return drm_memrange_create_tail_node(mm, start, size); } -/** - * Walks the list of allocated memory ranges and calls the callback on - * one. - */ -int drm_memrange_for_each(struct drm_memrange *mm, - int (*callback)(struct drm_memrange_node *node, - void *data), - void *data) -{ - struct list_head *list, *next; - - list_for_each_safe(list, next, &mm->ml_entry) { - struct drm_memrange_node *cur; - int ret; - - cur = list_entry(list, struct drm_memrange_node, ml_entry); - if (!cur->free) { - ret = callback(cur, data); - if (ret != 0) - return ret; - } - } - - return 0; -} -EXPORT_SYMBOL(drm_memrange_for_each); - EXPORT_SYMBOL(drm_memrange_init); void drm_memrange_takedown(struct drm_memrange * mm) diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index a1ce139b..919f32d7 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -288,45 +288,17 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, } static int -evict_callback(struct drm_memrange_node *node, void *data) -{ - struct drm_gem_object *obj; - struct drm_i915_gem_object *obj_priv; - - DRM_INFO ("evict node %p\n", node); - - obj = node->private; - DRM_INFO ("evict obj %p\n", obj); - - obj_priv = obj->driver_private; - DRM_INFO ("evict priv %p\n", obj_priv); - - DRM_INFO ("pin_count %d\n", obj_priv->pin_count); - if (obj_priv->pin_count == 0) - i915_gem_object_unbind(obj); - - return 0; -} - -static int -i915_gem_sync_and_evict(struct drm_device *dev) +i915_gem_sync(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; - int ret; RING_LOCALS; BEGIN_LP_RING(2); OUT_RING(CMD_MI_FLUSH | MI_READ_FLUSH | MI_EXE_FLUSH); OUT_RING(0); /* noop */ ADVANCE_LP_RING(); - ret = i915_quiescent(dev); - if (ret != 0) - return ret; - - /* Evict everything so we have space for sure. */ - drm_memrange_for_each(&dev_priv->mm.gtt_space, evict_callback, NULL); - return 0; + return i915_quiescent(dev); } static int @@ -410,9 +382,9 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* Big hammer: flush and idle the hardware so we can map things in/out. */ - ret = i915_gem_sync_and_evict(dev); + ret = i915_gem_sync(dev); if (ret != 0) { - DRM_ERROR ("i915_gem_sync_and_evict failed %d\n", ret); + DRM_ERROR ("i915_gem_sync failed %d\n", ret); return ret; } @@ -477,9 +449,18 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, args->buffer_count, ret); /* Clean up and return */ - ret = i915_gem_sync_and_evict(dev); + ret = i915_gem_sync(dev); if (ret) - DRM_ERROR ("failed to sync/evict buffers %d\n", ret); + DRM_ERROR ("failed to sync %d\n", ret); + + /* Evict all the buffers we moved in, leaving room for the next guy. */ + for (i = 0; i < args->buffer_count; i++) { + struct drm_gem_object *obj = object_list[i]; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + if (obj_priv->pin_count == 0) + i915_gem_object_unbind(obj); + } err: if (object_list != NULL) { for (i = 0; i < args->buffer_count; i++) -- cgit v1.2.3 From 2c8f970baaba9c72c882677f40ce8271bff03bac Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 5 May 2008 17:17:19 -0700 Subject: Unlock pages right after getting them. pages come back from find_or_create_page locked, but must not stay locked for long. Unlock them immediately instead of waiting until we're done with them to avoid deadlock when applications try to touch them. --- linux-core/i915_gem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index fabbaf9b..e3864612 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -61,7 +61,6 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) for (i = 0; i < page_count; i++) { if (obj_priv->page_list[i] != NULL) { - unlock_page (obj_priv->page_list[i]); page_cache_release (obj_priv->page_list[i]); } } @@ -109,7 +108,7 @@ i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where) DRM_INFO ("%s: object at offset %08x\n", where, obj_priv->gtt_offset); for (i = 0; i < len/4; i++) - DRM_INFO ("%3d: mem %08x gtt %08x\n", i, mem[i], readl(gtt + i)); + DRM_INFO ("%04x: mem %08x gtt %08x\n", i * 4, mem[i], readl(gtt + i)); iounmap (gtt); kunmap_atomic (mem, KM_USER0); } @@ -173,6 +172,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) obj_priv->gtt_space = NULL; return -ENOMEM; } + unlock_page (obj_priv->page_list[i]); } drm_ttm_cache_flush (obj_priv->page_list, page_count); -- cgit v1.2.3 From 91cba3ae17eb34d1836164f86c13a2a8e08c2a29 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 5 May 2008 22:10:02 -0700 Subject: Dump last batch buffer when hardware lockup is detected. --- linux-core/i915_gem.c | 87 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 25 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index e3864612..4dcfd2b1 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -30,6 +30,9 @@ #include "i915_drm.h" #include "i915_drv.h" +#define WATCH_BUF 0 +#define WATCH_EXEC 0 + int i915_gem_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -78,7 +81,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; -#if 0 +#if WATCH_BUF DRM_INFO ("%s:%d %p\n", __FUNCTION__, __LINE__, obj); DRM_INFO ("gtt_space %p\n", obj_priv->gtt_space); #endif @@ -97,20 +100,44 @@ i915_gem_object_unbind(struct drm_gem_object *obj) } static void -i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where) +i915_gem_dump_page (struct page *page, uint32_t start, uint32_t end, uint32_t bias, uint32_t mark) +{ + uint32_t *mem = kmap_atomic (page, KM_USER0); + int i; + for (i = start; i < end; i += 4) + DRM_INFO ("%08x: %08x%s\n", + (int) (bias + i), mem[i / 4], + (bias + i == mark) ? " ********" : ""); + kunmap_atomic (mem, KM_USER0); + /* give syslog time to catch up */ + msleep (1); +} + +static void +i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where, uint32_t mark) { - struct drm_device *dev = obj->dev; struct drm_i915_gem_object *obj_priv = obj->driver_private; - uint32_t *mem = kmap_atomic (obj_priv->page_list[0], KM_USER0); - volatile uint32_t *gtt = ioremap(dev->agp->base + obj_priv->gtt_offset, - PAGE_SIZE); - int i; + int page; DRM_INFO ("%s: object at offset %08x\n", where, obj_priv->gtt_offset); - for (i = 0; i < len/4; i++) - DRM_INFO ("%04x: mem %08x gtt %08x\n", i * 4, mem[i], readl(gtt + i)); - iounmap (gtt); - kunmap_atomic (mem, KM_USER0); + for (page = 0; page < (len + PAGE_SIZE-1) / PAGE_SIZE; page++) + { + int page_len, chunk, chunk_len; + + page_len = len - page * PAGE_SIZE; + if (page_len > PAGE_SIZE) + page_len = PAGE_SIZE; + + for (chunk = 0; chunk < page_len; chunk += 128) { + chunk_len = page_len - chunk; + if (chunk_len > 128) + chunk_len = 128; + i915_gem_dump_page (obj_priv->page_list[page], + chunk, chunk + chunk_len, + obj_priv->gtt_offset + page * PAGE_SIZE, + mark); + } + } } /** @@ -145,7 +172,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) obj_priv->gtt_space->private = obj; obj_priv->gtt_offset = obj_priv->gtt_space->start; -#if 0 +#if WATCH_BUF DRM_INFO ("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); #endif @@ -274,7 +301,7 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, (reloc_offset & (PAGE_SIZE - 1))); reloc_val = target_obj_priv->gtt_offset + reloc.delta; -#if 0 +#if WATCH_BUF DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n", obj, (unsigned int) reloc.offset, readl (reloc_entry), reloc_val); @@ -285,7 +312,9 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, drm_gem_object_unreference (target_obj); } -/* i915_gem_dump_object (obj, 128, __FUNCTION__); */ +#if WATCH_BUF + i915_gem_dump_object (obj, 128, __FUNCTION__, ~0); +#endif return 0; } @@ -323,13 +352,6 @@ i915_dispatch_gem_execbuffer (struct drm_device * dev, return -EINVAL; } - i915_kernel_lost_context(dev); - -#if 0 - DRM_INFO ("execbuffer at %x+%d len %d\n", - (uint32_t) exec_offset, exec->batch_start_offset, exec_len); -#endif - if (!exec_start) return -EINVAL; @@ -380,9 +402,9 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, LOCK_TEST_WITH_RETURN(dev, file_priv); -#if 0 - DRM_INFO ("buffers_ptr %d buffer_count %d\n", - (int) args->buffers_ptr, args->buffer_count); +#if WATCH_EXEC + DRM_INFO ("buffers_ptr %d buffer_count %d len %08x\n", + (int) args->buffers_ptr, args->buffer_count, args->batch_len); #endif i915_kernel_lost_context(dev); @@ -437,6 +459,13 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* make sure all previous memory operations have passed */ +#if WATCH_EXEC + i915_gem_dump_object (object_list[args->buffer_count - 1], + args->batch_len, + __FUNCTION__, + ~0); +#endif + /* Exec the batchbuffer */ ret = i915_dispatch_gem_execbuffer (dev, args, exec_offset); if (ret) @@ -457,7 +486,15 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* Clean up and return */ ret = i915_gem_sync(dev); if (ret) - DRM_ERROR ("failed to sync %d\n", ret); + { + drm_i915_private_t *dev_priv = dev->dev_private; + uint32_t acthd = I915_READ(IS_I965G(dev) ? I965REG_ACTHD : I915REG_ACTHD); + DRM_ERROR ("failed to sync %d acthd %08x\n", ret, acthd); + i915_gem_dump_object (object_list[args->buffer_count - 1], + args->batch_len, + __FUNCTION__, + acthd); + } /* Evict all the buffers we moved in, leaving room for the next guy. */ for (i = 0; i < args->buffer_count; i++) { -- cgit v1.2.3 From 8551bfc6dba03dcd9d182b2099a0906153ecfa01 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 6 May 2008 11:18:47 -0700 Subject: GEM: Save the last ioremapped page for relocations in case we need it again. --- linux-core/i915_gem.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 4dcfd2b1..e795ee77 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -233,6 +233,8 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, struct drm_i915_gem_relocation_entry __user *relocs; struct drm_i915_gem_object *obj_priv = obj->driver_private; int i; + uint32_t last_reloc_offset = -1; + void *reloc_page = NULL; /* Choose the GTT offset for our buffer and put it there. */ if (obj_priv->gtt_space == NULL) { @@ -249,7 +251,6 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, for (i = 0; i < entry->relocation_count; i++) { struct drm_gem_object *target_obj; struct drm_i915_gem_object *target_obj_priv; - void *reloc_page; uint32_t reloc_val, reloc_offset, *reloc_entry; int ret; @@ -288,13 +289,20 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, * perform. */ reloc_offset = obj_priv->gtt_offset + reloc.offset; - reloc_page = ioremap(dev->agp->base + - (reloc_offset & ~(PAGE_SIZE - 1)), - PAGE_SIZE); - if (reloc_page == NULL) - { - drm_gem_object_unreference (target_obj); - return -ENOMEM; + if (reloc_page == NULL || + (last_reloc_offset & ~(PAGE_SIZE - 1)) != + (reloc_offset & ~(PAGE_SIZE - 1))) { + if (reloc_page != NULL) + iounmap(reloc_page); + + reloc_page = ioremap(dev->agp->base + + (reloc_offset & ~(PAGE_SIZE - 1)), + PAGE_SIZE); + last_reloc_offset = reloc_offset; + if (reloc_page == NULL) { + drm_gem_object_unreference (target_obj); + return -ENOMEM; + } } reloc_entry = (uint32_t *)((char *)reloc_page + @@ -308,10 +316,12 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, #endif writel (reloc_val, reloc_entry); - iounmap(reloc_page); drm_gem_object_unreference (target_obj); } + if (reloc_page != NULL) + iounmap(reloc_page); + #if WATCH_BUF i915_gem_dump_object (obj, 128, __FUNCTION__, ~0); #endif -- cgit v1.2.3 From dd6976c56f9f14ea8aa630833e9cc9711157d74f Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 6 May 2008 11:25:53 -0700 Subject: GEM: Skip relocation if presumed offset matches. --- linux-core/i915_gem.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index e795ee77..4534dae2 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -285,6 +285,9 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, return -EINVAL; } + if (target_obj_priv->gtt_offset == reloc.presumed_offset) + continue; + /* Map the page containing the relocation we're going to * perform. */ -- cgit v1.2.3 From d2373b2a341868882208bb4297ab4f2f51302031 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 6 May 2008 13:28:26 -0700 Subject: GEM: Use irq-based fencing rather than syncing and evicting every exec. --- linux-core/i915_gem.c | 175 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 130 insertions(+), 45 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 4534dae2..d630b0b2 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -73,6 +73,31 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) obj_priv->page_list = NULL; } +/** + * Ensures that all rendering to the object has completed and the object is + * safe to unbind from the GTT. + */ +static int +i915_gem_object_wait_rendering(struct drm_gem_object *obj) +{ + struct drm_device *dev = obj->dev; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + int ret; + + /* If there is rendering queued on the buffer being evicted, wait for + * it. + */ + if (obj_priv->last_rendering_cookie != 0) { + ret = i915_wait_irq(dev, obj_priv->last_rendering_cookie); + if (ret != 0) + return ret; + /* Clear it now that we know it's passed. */ + obj_priv->last_rendering_cookie = 0; + } + + return 0; +} + /** * Unbinds an object from the GTT aperture. */ @@ -88,6 +113,8 @@ i915_gem_object_unbind(struct drm_gem_object *obj) if (obj_priv->gtt_space == NULL) return; + i915_gem_object_wait_rendering(obj); + if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); @@ -97,8 +124,10 @@ i915_gem_object_unbind(struct drm_gem_object *obj) drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; + list_del_init(&obj_priv->gtt_lru_entry); } +#if 0 static void i915_gem_dump_page (struct page *page, uint32_t start, uint32_t end, uint32_t bias, uint32_t mark) { @@ -139,6 +168,39 @@ i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where, ui } } } +#endif + +static int +i915_gem_evict_something(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + int ret; + + /* Find the LRU buffer. */ + BUG_ON(!list_empty(&dev_priv->mm.gtt_lru)); + obj_priv = list_entry(dev_priv->mm.gtt_lru.prev, + struct drm_i915_gem_object, + gtt_lru_entry); + obj = obj_priv->obj; + + /* Only unpinned buffers should be on this list. */ + BUG_ON(obj_priv->pin_count != 0); + + /* Do this separately from the wait_rendering in + * i915_gem_object_unbind() because we want to catch interrupts and + * return. + */ + ret = i915_gem_object_wait_rendering(obj); + if (ret != 0) + return ret; + + /* Wait on the rendering and unbind the buffer. */ + i915_gem_object_unbind(obj); + + return 0; +} /** * Finds free space in the GTT aperture and binds the object there. @@ -150,7 +212,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; struct drm_memrange_node *free_space; - int page_count, i; + int page_count, i, ret; if (alignment == 0) alignment = PAGE_SIZE; @@ -159,18 +221,31 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) return -EINVAL; } + search_free: free_space = drm_memrange_search_free(&dev_priv->mm.gtt_space, obj->size, alignment, 0); - if (free_space == NULL) - return -ENOMEM; - obj_priv->gtt_space = drm_memrange_get_block(free_space, - obj->size, - alignment); - if (obj_priv->gtt_space == NULL) - return -ENOMEM; - obj_priv->gtt_space->private = obj; - obj_priv->gtt_offset = obj_priv->gtt_space->start; + if (free_space != NULL) { + obj_priv->gtt_space = + drm_memrange_get_block(free_space, obj->size, + alignment); + if (obj_priv->gtt_space != NULL) { + obj_priv->gtt_space->private = obj; + obj_priv->gtt_offset = obj_priv->gtt_space->start; + } + } + if (obj_priv->gtt_space == NULL) { + /* If the gtt is empty and we're still having trouble + * fitting our object in, we're out of memory. + */ + if (list_empty(&dev_priv->mm.gtt_lru)) + return -ENOMEM; + + ret = i915_gem_evict_something(dev); + if (ret != 0) + return ret; + goto search_free; + } #if WATCH_BUF DRM_INFO ("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); @@ -229,6 +304,7 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, struct drm_i915_gem_validate_entry *entry) { struct drm_device *dev = obj->dev; + drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_relocation_entry reloc; struct drm_i915_gem_relocation_entry __user *relocs; struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -244,6 +320,12 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, } entry->buffer_offset = obj_priv->gtt_offset; + if (obj_priv->pin_count == 0) { + /* Move our buffer to the head of the LRU. */ + list_del_init(&obj_priv->gtt_lru_entry); + list_add(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); + } + relocs = (struct drm_i915_gem_relocation_entry __user *) (uintptr_t) entry->relocs_ptr; /* Apply the relocations, using the GTT aperture to avoid cache * flushing requirements. @@ -331,8 +413,8 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, return 0; } -static int -i915_gem_sync(struct drm_device *dev) +static void +i915_gem_flush(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; RING_LOCALS; @@ -341,8 +423,6 @@ i915_gem_sync(struct drm_device *dev) OUT_RING(CMD_MI_FLUSH | MI_READ_FLUSH | MI_EXE_FLUSH); OUT_RING(0); /* noop */ ADVANCE_LP_RING(); - - return i915_quiescent(dev); } static int @@ -412,6 +492,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_gem_object **object_list; int ret, i; uint64_t exec_offset; + uint32_t cookie; LOCK_TEST_WITH_RETURN(dev, file_priv); @@ -421,14 +502,6 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, #endif i915_kernel_lost_context(dev); - /* Big hammer: flush and idle the hardware so we can map things in/out. - */ - ret = i915_gem_sync(dev); - if (ret != 0) { - DRM_ERROR ("i915_gem_sync failed %d\n", ret); - return ret; - } - /* Copy in the validate list from userland */ validate_list = drm_calloc(sizeof(*validate_list), args->buffer_count, DRM_MEM_DRIVER); @@ -468,6 +541,21 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } } + for (i = 0; i < args->buffer_count; i++) { + struct drm_gem_object *obj = object_list[i]; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + if (obj_priv->gtt_space == NULL) { + /* We evicted the buffer in the process of validating + * our set of buffers in. We could try to recover by + * kicking them everything out and trying again from + * the start. + */ + ret = -ENOMEM; + goto err; + } + } + exec_offset = validate_list[args->buffer_count - 1].buffer_offset; /* make sure all previous memory operations have passed */ @@ -487,6 +575,25 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } + /* Flush the rendering. We want this flush to go away, which will + * require intelligent cache management. + */ + i915_gem_flush(dev); + + /* Get a cookie representing the flush of the current buffer, which we + * can wait on. We would like to mitigate these interrupts, likely by + * only flushing occasionally (so that we have *some* interrupts + * representing completion of buffers that we can wait on when trying + * to clear up gtt space). + */ + cookie = i915_emit_irq(dev); + for (i = 0; i < args->buffer_count; i++) { + struct drm_gem_object *obj = object_list[i]; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + obj_priv->last_rendering_cookie = cookie; + } + /* Copy the new buffer offsets back to the user's validate list. */ ret = copy_to_user((struct drm_i915_relocation_entry __user*)(uintptr_t) args->buffers_ptr, @@ -495,28 +602,6 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, if (ret) DRM_ERROR ("failed to copy %d validate entries back to user (%d)\n", args->buffer_count, ret); - - /* Clean up and return */ - ret = i915_gem_sync(dev); - if (ret) - { - drm_i915_private_t *dev_priv = dev->dev_private; - uint32_t acthd = I915_READ(IS_I965G(dev) ? I965REG_ACTHD : I915REG_ACTHD); - DRM_ERROR ("failed to sync %d acthd %08x\n", ret, acthd); - i915_gem_dump_object (object_list[args->buffer_count - 1], - args->batch_len, - __FUNCTION__, - acthd); - } - - /* Evict all the buffers we moved in, leaving room for the next guy. */ - for (i = 0; i < args->buffer_count; i++) { - struct drm_gem_object *obj = object_list[i]; - struct drm_i915_gem_object *obj_priv = obj->driver_private; - - if (obj_priv->pin_count == 0) - i915_gem_object_unbind(obj); - } err: if (object_list != NULL) { for (i = 0; i < args->buffer_count; i++) @@ -595,7 +680,7 @@ int i915_gem_init_object(struct drm_gem_object *obj) return -ENOMEM; obj->driver_private = obj_priv; - + INIT_LIST_HEAD(&obj_priv->gtt_lru_entry); return 0; } -- cgit v1.2.3 From 631e86c5c4ad9b2cdd40749ea3b351204a362c80 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 6 May 2008 14:43:49 -0700 Subject: Start coding up memory domains --- linux-core/drmP.h | 24 +++++++++++++++++++++++- linux-core/drm_gem.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index ffeafc18..cdeecc30 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -643,6 +643,15 @@ struct drm_gem_object { */ int name; + /** + * Memory domains. These monitor which caches contain read/write data + * related to the object. When transitioning from one set of domains + * to another, the driver is called to ensure that caches are suitably + * flushed and invalidated + */ + uint32_t read_domains; + uint32_t write_domain; + void *driver_private; }; @@ -942,6 +951,8 @@ struct drm_device { spinlock_t object_name_lock; struct idr object_name_idr; atomic_t object_count; + uint32_t invalidate_domains; /* domains pending invalidation */ + uint32_t flush_domains; /* domains pending flush */ /*@} */ }; @@ -1321,6 +1332,7 @@ static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) return block->mm; } +/* Graphics Execution Manager library functions (drm_gem.c) */ int drm_gem_init (struct drm_device *dev); @@ -1330,7 +1342,6 @@ drm_gem_object_free (struct kref *kref); void drm_gem_object_handle_free (struct kref *kref); -/* Graphics Execution Manager library functions (drm_gem.c) */ static inline void drm_gem_object_reference(struct drm_gem_object *obj) { kref_get(&obj->refcount); @@ -1385,6 +1396,17 @@ int drm_gem_open_ioctl(struct drm_device *dev, void *data, void drm_gem_open(struct drm_device *dev, struct drm_file *file_private); void drm_gem_release(struct drm_device *dev, struct drm_file *file_private); + +/* + * Given the new read/write domains for an object, + * compute the invalidate/flush domains for the whole device. + * + */ +int drm_gem_object_set_domain (struct drm_gem_object *object, + uint32_t read_domains, + uint32_t write_domains); + + extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev); extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev); diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 80d5a350..929c008f 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -539,3 +539,41 @@ drm_gem_object_handle_free (struct kref *kref) } EXPORT_SYMBOL(drm_gem_object_handle_free); + +/* + * Set the next domain for the specified object. This + * may not actually perform the necessary flushing/invaliding though, + * as that may want to be batched with other set_domain operations + */ +int drm_gem_object_set_domain (struct drm_gem_object *obj, + uint32_t read_domains, + uint32_t write_domain) +{ + struct drm_device *dev = obj->dev; + uint32_t invalidate_domains = 0; + uint32_t flush_domains = 0; + + /* + * Flush the current write domain if + * the new read domains don't match. Invalidate + * any read domains which differ from the old + * write domain + */ + if (obj->write_domain && obj->write_domain != read_domains) + { + flush_domains |= obj->write_domain; + invalidate_domains |= read_domains & ~obj->write_domain; + } + /* + * Invalidate any read caches which may have + * stale data. That is, any new read domains. + */ + invalidate_domains |= read_domains & ~obj->read_domains; + obj->write_domain = write_domain; + obj->read_domain = read_domains; + if ((flush_domains | invalidate_domains) & DRM_GEM_DOMAIN_CPU) + drm_gem_object_clflush (obj); + dev->invalidate_domains |= invalidate_domains & ~DRM_GEM_DOMAIN_CPU; + dev->flush_domains |= flush_domains & ~DRM_GEM_DOMAIN_CPU; +} +EXPORT_SYMBOL(drm_gem_object_set_domain); -- cgit v1.2.3 From 61253f4f677518537368103799c9510b8b5ad1e3 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 6 May 2008 20:00:23 -0700 Subject: [intel-GEM] Add memory domain support. Memory domains allow the kernel to track which caches to flush and how to move objects before buffer execution. --- linux-core/drm_gem.c | 41 ++----------- linux-core/i915_gem.c | 156 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 134 insertions(+), 63 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 929c008f..e2272f27 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -325,6 +325,10 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, drm_gem_object_unreference(obj); if (IS_ERR((void *)addr)) return (int) addr; + + /* XXX hack until we have a driver callback to make this work */ + obj->read_domains = DRM_GEM_DOMAIN_CPU; + obj->write_domain = DRM_GEM_DOMAIN_CPU; args->addr_ptr = (uint64_t) addr; @@ -540,40 +544,3 @@ drm_gem_object_handle_free (struct kref *kref) } EXPORT_SYMBOL(drm_gem_object_handle_free); -/* - * Set the next domain for the specified object. This - * may not actually perform the necessary flushing/invaliding though, - * as that may want to be batched with other set_domain operations - */ -int drm_gem_object_set_domain (struct drm_gem_object *obj, - uint32_t read_domains, - uint32_t write_domain) -{ - struct drm_device *dev = obj->dev; - uint32_t invalidate_domains = 0; - uint32_t flush_domains = 0; - - /* - * Flush the current write domain if - * the new read domains don't match. Invalidate - * any read domains which differ from the old - * write domain - */ - if (obj->write_domain && obj->write_domain != read_domains) - { - flush_domains |= obj->write_domain; - invalidate_domains |= read_domains & ~obj->write_domain; - } - /* - * Invalidate any read caches which may have - * stale data. That is, any new read domains. - */ - invalidate_domains |= read_domains & ~obj->read_domains; - obj->write_domain = write_domain; - obj->read_domain = read_domains; - if ((flush_domains | invalidate_domains) & DRM_GEM_DOMAIN_CPU) - drm_gem_object_clflush (obj); - dev->invalidate_domains |= invalidate_domains & ~DRM_GEM_DOMAIN_CPU; - dev->flush_domains |= flush_domains & ~DRM_GEM_DOMAIN_CPU; -} -EXPORT_SYMBOL(drm_gem_object_set_domain); diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index d630b0b2..911f9aa8 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -30,8 +30,8 @@ #include "i915_drm.h" #include "i915_drv.h" -#define WATCH_BUF 0 -#define WATCH_EXEC 0 +#define WATCH_BUF 1 +#define WATCH_EXEC 1 int i915_gem_init_ioctl(struct drm_device *dev, void *data, @@ -73,9 +73,33 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) obj_priv->page_list = NULL; } +static void +i915_gem_flush(struct drm_device *dev, uint32_t domains) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + uint32_t cmd; + RING_LOCALS; + +#if WATCH_EXEC + DRM_INFO ("%s: flush %08x\n", __FUNCTION__, domains); +#endif + cmd = CMD_MI_FLUSH | MI_NO_WRITE_FLUSH; + if (domains & DRM_GEM_DOMAIN_I915_RENDER) + cmd &= ~MI_NO_WRITE_FLUSH; + if (domains & DRM_GEM_DOMAIN_I915_SAMPLER) + cmd |= MI_READ_FLUSH; + if (domains & DRM_GEM_DOMAIN_I915_INSTRUCTION) + cmd |= MI_EXE_FLUSH; + + BEGIN_LP_RING(2); + OUT_RING(cmd); + OUT_RING(0); /* noop */ + ADVANCE_LP_RING(); +} + /** * Ensures that all rendering to the object has completed and the object is - * safe to unbind from the GTT. + * safe to unbind from the GTT or access from the CPU. */ static int i915_gem_object_wait_rendering(struct drm_gem_object *obj) @@ -84,10 +108,27 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) struct drm_i915_gem_object *obj_priv = obj->driver_private; int ret; + /* If there are writes queued to the buffer, flush and + * create a new cookie to wait for. + */ + if (obj->write_domain & ~(DRM_GEM_DOMAIN_CPU)) + { +#if WATCH_BUF + DRM_INFO ("%s: flushing object %p from write domain %08x\n", + __FUNCTION__, obj, obj->write_domain); +#endif + i915_gem_flush (dev, obj->write_domain); + obj->write_domain = 0; + obj_priv->last_rendering_cookie = i915_emit_irq (dev); + } /* If there is rendering queued on the buffer being evicted, wait for * it. */ if (obj_priv->last_rendering_cookie != 0) { +#if WATCH_BUF + DRM_INFO ("%s: object %p wait for cookie %08x\n", + __FUNCTION__, obj, obj_priv->last_rendering_cookie); +#endif ret = i915_wait_irq(dev, obj_priv->last_rendering_cookie); if (ret != 0) return ret; @@ -125,6 +166,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; list_del_init(&obj_priv->gtt_lru_entry); + drm_gem_object_unreference (obj); } #if 0 @@ -277,10 +319,6 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) unlock_page (obj_priv->page_list[i]); } - drm_ttm_cache_flush (obj_priv->page_list, page_count); - DRM_MEMORYBARRIER(); - drm_agp_chipset_flush(dev); - /* Create an AGP memory structure pointing at our pages, and bind it * into the GTT. */ @@ -298,6 +336,73 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) return 0; } +static void +i915_gem_clflush_object (struct drm_gem_object *obj) +{ + struct drm_device *dev = obj->dev; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + drm_ttm_cache_flush (obj_priv->page_list, obj->size / PAGE_SIZE); + drm_agp_chipset_flush(dev); +} + +/* + * Set the next domain for the specified object. This + * may not actually perform the necessary flushing/invaliding though, + * as that may want to be batched with other set_domain operations + */ +static void +i915_gem_object_set_domain (struct drm_gem_object *obj, + uint32_t read_domains, + uint32_t write_domain) +{ + struct drm_device *dev = obj->dev; + uint32_t invalidate_domains = 0; + uint32_t flush_domains = 0; + +#if WATCH_BUF + DRM_INFO ("%s: object %p read %08x write %08x\n", + __FUNCTION__, obj, read_domains, write_domain); +#endif + /* + * Flush the current write domain if + * the new read domains don't match. Invalidate + * any read domains which differ from the old + * write domain + */ + if (obj->write_domain && obj->write_domain != read_domains) + { + flush_domains |= obj->write_domain; + invalidate_domains |= read_domains & ~obj->write_domain; + } + /* + * Invalidate any read caches which may have + * stale data. That is, any new read domains. + */ + invalidate_domains |= read_domains & ~obj->read_domains; + if ((flush_domains | invalidate_domains) & DRM_GEM_DOMAIN_CPU) + { +#if WATCH_BUF + DRM_INFO ("%s: CPU domain flush %08x invalidate %08x\n", + __FUNCTION__, flush_domains, invalidate_domains); +#endif + /* + * If we're invaliding the CPU cache and flushing a GPU cache, + * then pause for rendering so that the GPU caches will be + * flushed before the cpu cache is invalidated + */ + if ((invalidate_domains & DRM_GEM_DOMAIN_CPU) && + (flush_domains & ~DRM_GEM_DOMAIN_CPU)) + i915_gem_object_wait_rendering (obj); + i915_gem_clflush_object (obj); + } + + obj->write_domain = write_domain; + obj->read_domains = read_domains; + dev->invalidate_domains |= invalidate_domains & ~DRM_GEM_DOMAIN_CPU; + dev->flush_domains |= flush_domains & ~DRM_GEM_DOMAIN_CPU; +} + static int i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, struct drm_file *file_priv, @@ -318,12 +423,17 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, if (obj_priv->gtt_space == NULL) return -ENOMEM; } + + /* Do domain migration */ + i915_gem_object_set_domain (obj, entry->read_domains, entry->write_domain); + entry->buffer_offset = obj_priv->gtt_offset; if (obj_priv->pin_count == 0) { /* Move our buffer to the head of the LRU. */ list_del_init(&obj_priv->gtt_lru_entry); list_add(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); + drm_gem_object_reference (obj); } relocs = (struct drm_i915_gem_relocation_entry __user *) (uintptr_t) entry->relocs_ptr; @@ -413,18 +523,6 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, return 0; } -static void -i915_gem_flush(struct drm_device *dev) -{ - drm_i915_private_t *dev_priv = dev->dev_private; - RING_LOCALS; - - BEGIN_LP_RING(2); - OUT_RING(CMD_MI_FLUSH | MI_READ_FLUSH | MI_EXE_FLUSH); - OUT_RING(0); /* noop */ - ADVANCE_LP_RING(); -} - static int i915_dispatch_gem_execbuffer (struct drm_device * dev, struct drm_i915_gem_execbuffer * exec, @@ -556,6 +654,17 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } } + if (dev->invalidate_domains | dev->flush_domains) + { +#if WATCH_EXEC + DRM_INFO ("%s: invalidate_domains %08x flush_domains %08x\n", + __FUNCTION__, dev->invalidate_domains, dev->flush_domains); +#endif + i915_gem_flush (dev, dev->invalidate_domains | dev->flush_domains); + dev->invalidate_domains = 0; + dev->flush_domains = 0; + } + exec_offset = validate_list[args->buffer_count - 1].buffer_offset; /* make sure all previous memory operations have passed */ @@ -575,14 +684,9 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } - /* Flush the rendering. We want this flush to go away, which will - * require intelligent cache management. - */ - i915_gem_flush(dev); - - /* Get a cookie representing the flush of the current buffer, which we + /* Get a cookie representing the execution of the current buffer, which we * can wait on. We would like to mitigate these interrupts, likely by - * only flushing occasionally (so that we have *some* interrupts + * only creating cookies occasionally (so that we have *some* interrupts * representing completion of buffers that we can wait on when trying * to clear up gtt space). */ -- cgit v1.2.3 From 6a6c37af9ecaabfe1399a1300cadaff730767013 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 6 May 2008 21:59:06 -0700 Subject: [intel-GEM] ref count objects in gtt-lru. If objects on the lru aren't ref counted, they'll get pulled from the gtt as soon as they are freed. This change does cause objects to get stuck in the gtt until they're forced out by new requests. The lru should get cleaned when the irq occurs. --- linux-core/i915_gem.c | 62 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 13 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 911f9aa8..220d6179 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -30,8 +30,9 @@ #include "i915_drm.h" #include "i915_drv.h" -#define WATCH_BUF 1 -#define WATCH_EXEC 1 +#define WATCH_BUF 0 +#define WATCH_EXEC 0 +#define WATCH_LRU 0 int i915_gem_init_ioctl(struct drm_device *dev, void *data, @@ -165,11 +166,14 @@ i915_gem_object_unbind(struct drm_gem_object *obj) drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; - list_del_init(&obj_priv->gtt_lru_entry); - drm_gem_object_unreference (obj); + if (!list_empty (&obj_priv->gtt_lru_entry)) + { + list_del_init(&obj_priv->gtt_lru_entry); + drm_gem_object_unreference (obj); + } } -#if 0 +#if WATCH_BUF | WATCH_EXEC static void i915_gem_dump_page (struct page *page, uint32_t start, uint32_t end, uint32_t bias, uint32_t mark) { @@ -212,6 +216,21 @@ i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where, ui } #endif +#if WATCH_LRU +static void +i915_dump_lru (struct drm_device *dev, const char *where) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv; + + DRM_INFO ("GTT LRU %s {\n", where); + list_for_each_entry (obj_priv, &dev_priv->mm.gtt_lru, gtt_lru_entry) { + DRM_INFO (" %p: %08x\n", obj_priv, obj_priv->last_rendering_cookie); + } + DRM_INFO ("}\n"); +} +#endif + static int i915_gem_evict_something(struct drm_device *dev) { @@ -221,11 +240,14 @@ i915_gem_evict_something(struct drm_device *dev) int ret; /* Find the LRU buffer. */ - BUG_ON(!list_empty(&dev_priv->mm.gtt_lru)); - obj_priv = list_entry(dev_priv->mm.gtt_lru.prev, - struct drm_i915_gem_object, - gtt_lru_entry); + BUG_ON(list_empty(&dev_priv->mm.gtt_lru)); + obj_priv = list_first_entry(&dev_priv->mm.gtt_lru, + struct drm_i915_gem_object, + gtt_lru_entry); obj = obj_priv->obj; +#if WATCH_LRU + DRM_INFO ("%s: evicting %p\n", __FUNCTION__, obj); +#endif /* Only unpinned buffers should be on this list. */ BUG_ON(obj_priv->pin_count != 0); @@ -240,6 +262,9 @@ i915_gem_evict_something(struct drm_device *dev) /* Wait on the rendering and unbind the buffer. */ i915_gem_object_unbind(obj); +#if WATCH_LRU + DRM_INFO ("%s: evicted %p\n", __FUNCTION__, obj); +#endif return 0; } @@ -280,8 +305,13 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) /* If the gtt is empty and we're still having trouble * fitting our object in, we're out of memory. */ - if (list_empty(&dev_priv->mm.gtt_lru)) +#if WATCH_LRU + DRM_INFO ("%s: GTT full, evicting something\n", __FUNCTION__); +#endif + if (list_empty(&dev_priv->mm.gtt_lru)) { + DRM_ERROR ("GTT full, but LRU list empty\n"); return -ENOMEM; + } ret = i915_gem_evict_something(dev); if (ret != 0) @@ -431,9 +461,14 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, if (obj_priv->pin_count == 0) { /* Move our buffer to the head of the LRU. */ - list_del_init(&obj_priv->gtt_lru_entry); - list_add(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); - drm_gem_object_reference (obj); + if (list_empty (&obj_priv->gtt_lru_entry)) { + drm_gem_object_reference (obj); + list_add_tail(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); + } else + list_move_tail(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); +#if WATCH_LRU && 0 + i915_dump_lru (dev, __FUNCTION__); +#endif } relocs = (struct drm_i915_gem_relocation_entry __user *) (uintptr_t) entry->relocs_ptr; @@ -784,6 +819,7 @@ int i915_gem_init_object(struct drm_gem_object *obj) return -ENOMEM; obj->driver_private = obj_priv; + obj_priv->obj = obj; INIT_LIST_HEAD(&obj_priv->gtt_lru_entry); return 0; } -- cgit v1.2.3 From 5f5f01ed91f5ad50f2b38e11740a30441ac845a4 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 7 May 2008 12:46:06 -0700 Subject: GEM: Extend cache domain stuff for 965. One of our MI_FLUSH bits is reserved on 965, being always implied, and there's a vertex cache that was forgotten. --- linux-core/i915_gem.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 220d6179..f3adf39b 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -84,14 +84,41 @@ i915_gem_flush(struct drm_device *dev, uint32_t domains) #if WATCH_EXEC DRM_INFO ("%s: flush %08x\n", __FUNCTION__, domains); #endif + + /* read/write caches: + * DRM_GEM_DOMAIN_I915_RENDER is always invalidated, but is + * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is also + * flushed at 2d versus 3d pipeline switches. + * + * read-only caches: + * DRM_GEM_DOMAIN_I915_SAMPLER is flushed on pre-965 if MI_READ_FLUSH + * is set, and is always flushed on 965. + * DRM_GEM_DOMAIN_I915_COMMAND may not exist? + * DRM_GEM_DOMAIN_I915_INSTRUCTION, which exists on 965, is invalidated + * when MI_EXE_FLUSH is set. + * DRM_GEM_DOMAIN_I915_VERTEX, which exists on 965, is invalidated with + * every MI_FLUSH. + * + * TLBs: + * On 965, TLBs associated with DRM_GEM_DOMAIN_I915_COMMAND and + * DRM_GEM_DOMAIN_CPU in are invalidated at PTE write and + * DRM_GEM_DOMAIN_I915_RENDER and DRM_GEM_DOMAIN_I915_SAMPLER are + * flushed at any MI_FLUSH. + */ + cmd = CMD_MI_FLUSH | MI_NO_WRITE_FLUSH; if (domains & DRM_GEM_DOMAIN_I915_RENDER) cmd &= ~MI_NO_WRITE_FLUSH; - if (domains & DRM_GEM_DOMAIN_I915_SAMPLER) - cmd |= MI_READ_FLUSH; + if (!IS_I965G(dev)) { + /* On the 965, the sampler cache always gets flushed and this + * bit is reserved. + */ + if (domains & DRM_GEM_DOMAIN_I915_SAMPLER) + cmd |= MI_READ_FLUSH; + } if (domains & DRM_GEM_DOMAIN_I915_INSTRUCTION) cmd |= MI_EXE_FLUSH; - + BEGIN_LP_RING(2); OUT_RING(cmd); OUT_RING(0); /* noop */ @@ -363,6 +390,13 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) return -ENOMEM; } + /* When we have just bound an object, we have no valid read + * caches on it, regardless of where it was before. We also need + * an MI_FLUSH to occur so that the render and sampler TLBs + * get flushed and pick up our binding change above. + */ + obj->read_domains = 0; + return 0; } -- cgit v1.2.3 From 06e9761f94599c6378c8fa0cdbd1e1c1776bae7a Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 7 May 2008 14:10:04 -0700 Subject: GEM: Wait for existing rendering to complete before writing relocation data. This should already have been generally safe since we don't change contents and put in new relocations between execbufs, so if we were writing in a new relocation then we'd already waited rendering to complete when we moved the target of the relocation. However, doing the right thing will be required if we do buffer reuse. --- linux-core/i915_gem.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index f3adf39b..1d55eaa2 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -546,9 +546,18 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, return -EINVAL; } + /* If the relocation already has the right value in it, no + * more work needs to be done. + */ if (target_obj_priv->gtt_offset == reloc.presumed_offset) continue; + /* Now that we're going to actually write some data in, + * make sure that any rendering using this buffer's contents + * is completed. + */ + i915_gem_object_wait_rendering(obj); + /* Map the page containing the relocation we're going to * perform. */ -- cgit v1.2.3 From aa0621a19ffcdf2a92eac08fa4950e0423a8dd2b Mon Sep 17 00:00:00 2001 From: Arjan van de Ven Date: Wed, 7 May 2008 15:54:32 -0700 Subject: Apply a few stylistic cleanups to match kernel code. --- linux-core/drm_gem.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index e2272f27..4eaeffcf 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -217,8 +217,7 @@ drm_gem_alloc_ioctl(struct drm_device *dev, void *data, if (!(dev->driver->driver_features & DRIVER_GEM)) return -ENODEV; - /* Round requested size up to page size */ - args->size = (args->size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + args->size = roundup(args->size, PAGE_SIZE); /* Allocate the new object */ obj = drm_gem_object_alloc(dev, args->size); @@ -277,8 +276,8 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, offset = args->offset; - read = obj->filp->f_op->read(obj->filp, (char __user *)(uintptr_t)args->data_ptr, - args->size, &offset); + read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr, + args->size, &offset); if (read != args->size) { drm_gem_object_unreference(obj); if (read < 0) @@ -324,7 +323,7 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, up_write(¤t->mm->mmap_sem); drm_gem_object_unreference(obj); if (IS_ERR((void *)addr)) - return (int) addr; + return addr; /* XXX hack until we have a driver callback to make this work */ obj->read_domains = DRM_GEM_DOMAIN_CPU; @@ -358,8 +357,9 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, offset = args->offset; - written = obj->filp->f_op->write(obj->filp, (char __user *)(uintptr_t) args->data_ptr, - args->size, &offset); + written = vfs_write(obj->filp, + (char __user *)(uintptr_t) args->data_ptr, + args->size, &offset); if (written != args->size) { drm_gem_object_unreference(obj); if (written < 0) -- cgit v1.2.3 From 9af4c497433398fa4576a7c1c31036448cf4f24c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 8 May 2008 10:44:02 -0700 Subject: [intel-gem] Move domains to relocation records. add set_domain ioctl. Domain information is about buffer relationships, not buffer contents. That means a relocation contains the domain information as it knows how the source buffer references the target buffer. This also adds the set_domain ioctl so that user space can move buffers to the cpu domain. --- linux-core/drmP.h | 18 ++++ linux-core/drm_drv.c | 1 + linux-core/drm_gem.c | 35 +++++++- linux-core/i915_drv.c | 1 + linux-core/i915_gem.c | 235 ++++++++++++++++++++++++++++++++++++-------------- 5 files changed, 220 insertions(+), 70 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index cdeecc30..11688cdd 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -652,6 +652,15 @@ struct drm_gem_object { uint32_t read_domains; uint32_t write_domain; + /** + * While validating an exec operation, the + * new read/write domain values are computed here. + * They will be transferred to the above values + * at the point that any cache flushing occurs + */ + uint32_t pending_read_domains; + uint32_t pending_write_domain; + void *driver_private; }; @@ -765,6 +774,13 @@ struct drm_driver { int (*gem_init_object) (struct drm_gem_object *obj); void (*gem_free_object) (struct drm_gem_object *obj); + /** + * Driver-specific callback to set memory domains from userspace + */ + int (*gem_set_domain) (struct drm_gem_object *obj, + uint32_t read_domains, + uint32_t write_domain); + struct drm_fence_driver *fence_driver; struct drm_bo_driver *bo_driver; @@ -1392,6 +1408,8 @@ int drm_gem_name_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int drm_gem_open_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); +int drm_gem_set_domain_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); void drm_gem_open(struct drm_device *dev, struct drm_file *file_private); void drm_gem_release(struct drm_device *dev, struct drm_file *file_private); diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index 4c85cdf4..16c38dbe 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -158,6 +158,7 @@ static struct drm_ioctl_desc drm_ioctls[] = { DRM_IOCTL_DEF(DRM_IOCTL_GEM_MMAP, drm_gem_mmap_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_GEM_NAME, drm_gem_name_ioctl, DRM_AUTH), DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_SET_DOMAIN, drm_gem_set_domain_ioctl, DRM_AUTH), }; #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls ) diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 4eaeffcf..3673c933 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -325,10 +325,6 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, if (IS_ERR((void *)addr)) return addr; - /* XXX hack until we have a driver callback to make this work */ - obj->read_domains = DRM_GEM_DOMAIN_CPU; - obj->write_domain = DRM_GEM_DOMAIN_CPU; - args->addr_ptr = (uint64_t) addr; return 0; @@ -460,6 +456,37 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data, return 0; } +/** + * Called when user space prepares to use an object + */ +int +drm_gem_set_domain_ioctl (struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_gem_set_domain *args = data; + struct drm_gem_object *obj; + int ret; + + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + if (dev->driver->gem_set_domain) { + ret = dev->driver->gem_set_domain (obj, + args->read_domains, + args->write_domain); + } else { + obj->read_domains = args->read_domains; + obj->write_domain = args->write_domain; + ret = 0; + } + drm_gem_object_unreference (obj); + return ret; +} + /** * Called at device open time, sets up the structure for handling refcounting * of mm objects. diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index 3e788d25..ae8cf3e0 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -588,6 +588,7 @@ static struct drm_driver driver = { .ioctls = i915_ioctls, .gem_init_object = i915_gem_init_object, .gem_free_object = i915_gem_free_object, + .gem_set_domain = i915_gem_set_domain_ioctl, .fops = { .owner = THIS_MODULE, .open = drm_open, diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 1d55eaa2..861e7bb7 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -33,6 +33,7 @@ #define WATCH_BUF 0 #define WATCH_EXEC 0 #define WATCH_LRU 0 +#define WATCH_RELOC 0 int i915_gem_init_ioctl(struct drm_device *dev, void *data, @@ -75,54 +76,61 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) } static void -i915_gem_flush(struct drm_device *dev, uint32_t domains) +i915_gem_flush(struct drm_device *dev, uint32_t invalidate_domains, uint32_t flush_domains) { drm_i915_private_t *dev_priv = dev->dev_private; uint32_t cmd; RING_LOCALS; #if WATCH_EXEC - DRM_INFO ("%s: flush %08x\n", __FUNCTION__, domains); + DRM_INFO ("%s: invalidate %08x flush %08x\n", __FUNCTION__, + invalidate_domains, flush_domains); #endif - /* read/write caches: - * DRM_GEM_DOMAIN_I915_RENDER is always invalidated, but is - * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is also - * flushed at 2d versus 3d pipeline switches. - * - * read-only caches: - * DRM_GEM_DOMAIN_I915_SAMPLER is flushed on pre-965 if MI_READ_FLUSH - * is set, and is always flushed on 965. - * DRM_GEM_DOMAIN_I915_COMMAND may not exist? - * DRM_GEM_DOMAIN_I915_INSTRUCTION, which exists on 965, is invalidated - * when MI_EXE_FLUSH is set. - * DRM_GEM_DOMAIN_I915_VERTEX, which exists on 965, is invalidated with - * every MI_FLUSH. - * - * TLBs: - * On 965, TLBs associated with DRM_GEM_DOMAIN_I915_COMMAND and - * DRM_GEM_DOMAIN_CPU in are invalidated at PTE write and - * DRM_GEM_DOMAIN_I915_RENDER and DRM_GEM_DOMAIN_I915_SAMPLER are - * flushed at any MI_FLUSH. - */ - - cmd = CMD_MI_FLUSH | MI_NO_WRITE_FLUSH; - if (domains & DRM_GEM_DOMAIN_I915_RENDER) - cmd &= ~MI_NO_WRITE_FLUSH; - if (!IS_I965G(dev)) { - /* On the 965, the sampler cache always gets flushed and this - * bit is reserved. + if (flush_domains & DRM_GEM_DOMAIN_CPU) + drm_agp_chipset_flush(dev); + + if ((invalidate_domains|flush_domains) & ~DRM_GEM_DOMAIN_CPU) + { + /* read/write caches: + * DRM_GEM_DOMAIN_I915_RENDER is always invalidated, but is + * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is also + * flushed at 2d versus 3d pipeline switches. + * + * read-only caches: + * DRM_GEM_DOMAIN_I915_SAMPLER is flushed on pre-965 if MI_READ_FLUSH + * is set, and is always flushed on 965. + * DRM_GEM_DOMAIN_I915_COMMAND may not exist? + * DRM_GEM_DOMAIN_I915_INSTRUCTION, which exists on 965, is invalidated + * when MI_EXE_FLUSH is set. + * DRM_GEM_DOMAIN_I915_VERTEX, which exists on 965, is invalidated with + * every MI_FLUSH. + * + * TLBs: + * On 965, TLBs associated with DRM_GEM_DOMAIN_I915_COMMAND and + * DRM_GEM_DOMAIN_CPU in are invalidated at PTE write and + * DRM_GEM_DOMAIN_I915_RENDER and DRM_GEM_DOMAIN_I915_SAMPLER are + * flushed at any MI_FLUSH. */ - if (domains & DRM_GEM_DOMAIN_I915_SAMPLER) - cmd |= MI_READ_FLUSH; + + cmd = CMD_MI_FLUSH | MI_NO_WRITE_FLUSH; + if ((invalidate_domains|flush_domains) & DRM_GEM_DOMAIN_I915_RENDER) + cmd &= ~MI_NO_WRITE_FLUSH; + if (!IS_I965G(dev)) { + /* On the 965, the sampler cache always gets flushed and this + * bit is reserved. + */ + if (invalidate_domains & DRM_GEM_DOMAIN_I915_SAMPLER) + cmd |= MI_READ_FLUSH; + } + if (invalidate_domains & DRM_GEM_DOMAIN_I915_INSTRUCTION) + cmd |= MI_EXE_FLUSH; + + BEGIN_LP_RING(2); + OUT_RING(cmd); + OUT_RING(0); /* noop */ + ADVANCE_LP_RING(); } - if (domains & DRM_GEM_DOMAIN_I915_INSTRUCTION) - cmd |= MI_EXE_FLUSH; - - BEGIN_LP_RING(2); - OUT_RING(cmd); - OUT_RING(0); /* noop */ - ADVANCE_LP_RING(); } /** @@ -145,8 +153,10 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) DRM_INFO ("%s: flushing object %p from write domain %08x\n", __FUNCTION__, obj, obj->write_domain); #endif - i915_gem_flush (dev, obj->write_domain); + i915_gem_flush (dev, 0, obj->write_domain); obj->write_domain = 0; + if (obj_priv->last_rendering_cookie == 0) + drm_gem_object_reference (obj); obj_priv->last_rendering_cookie = i915_emit_irq (dev); } /* If there is rendering queued on the buffer being evicted, wait for @@ -162,6 +172,9 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) return ret; /* Clear it now that we know it's passed. */ obj_priv->last_rendering_cookie = 0; + + /* The cookie held a reference to the object, release that now */ + drm_gem_object_unreference (obj); } return 0; @@ -194,10 +207,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; if (!list_empty (&obj_priv->gtt_lru_entry)) - { list_del_init(&obj_priv->gtt_lru_entry); - drm_gem_object_unreference (obj); - } } #if WATCH_BUF | WATCH_EXEC @@ -403,11 +413,9 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) static void i915_gem_clflush_object (struct drm_gem_object *obj) { - struct drm_device *dev = obj->dev; struct drm_i915_gem_object *obj_priv = obj->driver_private; drm_ttm_cache_flush (obj_priv->page_list, obj->size / PAGE_SIZE); - drm_agp_chipset_flush(dev); } /* @@ -463,8 +471,32 @@ i915_gem_object_set_domain (struct drm_gem_object *obj, obj->write_domain = write_domain; obj->read_domains = read_domains; - dev->invalidate_domains |= invalidate_domains & ~DRM_GEM_DOMAIN_CPU; - dev->flush_domains |= flush_domains & ~DRM_GEM_DOMAIN_CPU; + dev->invalidate_domains |= invalidate_domains; + dev->flush_domains |= flush_domains; +} + +/** + * Once all of the objects have been set in the proper domain, + * perform the necessary flush and invalidate operations + */ + +static void +i915_gem_dev_set_domain (struct drm_device *dev) +{ + /* + * Now that all the buffers are synced to the proper domains, + * flush and invalidate the collected domains + */ + if (dev->invalidate_domains | dev->flush_domains) + { +#if WATCH_EXEC + DRM_INFO ("%s: invalidate_domains %08x flush_domains %08x\n", + __FUNCTION__, dev->invalidate_domains, dev->flush_domains); +#endif + i915_gem_flush (dev, dev->invalidate_domains, dev->flush_domains); + dev->invalidate_domains = 0; + dev->flush_domains = 0; + } } static int @@ -488,17 +520,13 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, return -ENOMEM; } - /* Do domain migration */ - i915_gem_object_set_domain (obj, entry->read_domains, entry->write_domain); - entry->buffer_offset = obj_priv->gtt_offset; if (obj_priv->pin_count == 0) { /* Move our buffer to the head of the LRU. */ - if (list_empty (&obj_priv->gtt_lru_entry)) { - drm_gem_object_reference (obj); + if (list_empty (&obj_priv->gtt_lru_entry)) list_add_tail(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); - } else + else list_move_tail(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); #if WATCH_LRU && 0 i915_dump_lru (dev, __FUNCTION__); @@ -536,16 +564,44 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, } if (reloc.offset > obj->size - 4) { - DRM_ERROR("Relocation beyond object bounds.\n"); + DRM_ERROR("Relocation beyond object bounds: obj %p target %d offset %d size %d.\n", + obj, reloc.target_handle, (int) reloc.offset, (int) obj->size); drm_gem_object_unreference (target_obj); return -EINVAL; } if (reloc.offset & 3) { - DRM_ERROR("Relocation not 4-byte aligned.\n"); + DRM_ERROR("Relocation not 4-byte aligned: obj %p target %d offset %d.\n", + obj, reloc.target_handle, (int) reloc.offset); drm_gem_object_unreference (target_obj); return -EINVAL; } + if (reloc.write_domain && target_obj->pending_write_domain && + reloc.write_domain != target_obj->pending_write_domain) + { + DRM_ERROR("Write domain conflict: obj %p target %d offset %d new %08x old %08x\n", + obj, reloc.target_handle, (int) reloc.offset, + reloc.write_domain, target_obj->pending_write_domain); + drm_gem_object_unreference (target_obj); + return -EINVAL; + } + +#if WATCH_RELOC + DRM_INFO ("%s: obj %p offset %08x target %d read %08x write %08x gtt %08x presumed %08x delta %08x\n", + __FUNCTION__, + obj, + (int) reloc.offset, + (int) reloc.target_handle, + (int) reloc.read_domains, + (int) reloc.write_domain, + (int) target_obj_priv->gtt_offset, + (int) reloc.presumed_offset, + reloc.delta); +#endif + + target_obj->pending_read_domains |= reloc.read_domains; + target_obj->pending_write_domain |= reloc.write_domain; + /* If the relocation already has the right value in it, no * more work needs to be done. */ @@ -558,6 +614,16 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, */ i915_gem_object_wait_rendering(obj); + /* As we're writing through the gtt, flush + * any CPU writes before we write the relocations + */ + if (obj->write_domain & DRM_GEM_DOMAIN_CPU) + { + i915_gem_clflush_object (obj); + drm_agp_chipset_flush(dev); + obj->write_domain = 0; + } + /* Map the page containing the relocation we're going to * perform. */ @@ -672,6 +738,19 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, LOCK_TEST_WITH_RETURN(dev, file_priv); +#if 0 + /* + * XXX wait for previous rendering to complete as we otherwise never + * flush the LRU list + */ + { + drm_i915_private_t *dev_priv = dev->dev_private; + + while (!list_empty (&dev_priv->mm.gtt_lru)) + i915_gem_evict_something (dev); + } +#endif + #if WATCH_EXEC DRM_INFO ("buffers_ptr %d buffer_count %d len %08x\n", (int) args->buffers_ptr, args->buffer_count, args->batch_len); @@ -717,6 +796,10 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } } + /* Set the pending read domains for the batch buffer to COMMAND */ + object_list[args->buffer_count-1]->pending_read_domains = DRM_GEM_DOMAIN_I915_COMMAND; + object_list[args->buffer_count-1]->pending_write_domain = 0; + for (i = 0; i < args->buffer_count; i++) { struct drm_gem_object *obj = object_list[i]; struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -730,22 +813,19 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, ret = -ENOMEM; goto err; } - } - if (dev->invalidate_domains | dev->flush_domains) - { -#if WATCH_EXEC - DRM_INFO ("%s: invalidate_domains %08x flush_domains %08x\n", - __FUNCTION__, dev->invalidate_domains, dev->flush_domains); -#endif - i915_gem_flush (dev, dev->invalidate_domains | dev->flush_domains); - dev->invalidate_domains = 0; - dev->flush_domains = 0; + /* make sure all previous memory operations have passed */ + i915_gem_object_set_domain (obj, + obj->pending_read_domains, + obj->pending_write_domain); + obj->pending_read_domains = 0; + obj->pending_write_domain = 0; } - exec_offset = validate_list[args->buffer_count - 1].buffer_offset; + /* Flush/invalidate caches and chipset buffer */ + i915_gem_dev_set_domain (dev); - /* make sure all previous memory operations have passed */ + exec_offset = validate_list[args->buffer_count - 1].buffer_offset; #if WATCH_EXEC i915_gem_dump_object (object_list[args->buffer_count - 1], @@ -773,6 +853,12 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_gem_object *obj = object_list[i]; struct drm_i915_gem_object *obj_priv = obj->driver_private; + /* + * Have the cookie hold a reference to this object + * which is freed when the object is waited for + */ + if (obj_priv->last_rendering_cookie == 0) + drm_gem_object_reference (obj); obj_priv->last_rendering_cookie = cookie; } @@ -789,6 +875,13 @@ err: for (i = 0; i < args->buffer_count; i++) drm_gem_object_unreference(object_list[i]); } + + /* XXX kludge for now as we don't clean the exec ring yet */ + if (object_list != NULL) { + for (i = 0; i < args->buffer_count; i++) + i915_gem_object_wait_rendering (object_list[i]); + } + drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, @@ -873,3 +966,13 @@ void i915_gem_free_object(struct drm_gem_object *obj) drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); } + +int +i915_gem_set_domain_ioctl (struct drm_gem_object *obj, + uint32_t read_domains, + uint32_t write_domain) +{ + i915_gem_object_set_domain (obj, read_domains, write_domain); + i915_gem_dev_set_domain (obj->dev); + return 0; +} -- cgit v1.2.3 From 2f573e6df4890784124eea24ce168702574f0152 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 8 May 2008 12:46:02 -0700 Subject: GEM: Fix oops on NULL dereference when we try clflushing when we don't need to. --- linux-core/i915_gem.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 861e7bb7..5c58032b 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -415,6 +415,13 @@ i915_gem_clflush_object (struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; + /* If we don't have a page list set up, then we're not pinned + * to GPU, and we can ignore the cache flush because it'll happen + * again at bind time. + */ + if (obj_priv->page_list == NULL) + return; + drm_ttm_cache_flush (obj_priv->page_list, obj->size / PAGE_SIZE); } -- cgit v1.2.3 From 07ad5ce1e199ebca1e51a831503f923fa49cc57e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 8 May 2008 11:13:29 -0700 Subject: Clean up whinging from checkpatch.pl in drm_gem.c Whitespace changes, a few too-long-lines and some extra braces. --- linux-core/drm_gem.c | 92 +++++++++++++++++++++++++++------------------------ linux-core/i915_gem.c | 2 ++ 2 files changed, 51 insertions(+), 43 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 3673c933..c85265ad 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -69,11 +69,11 @@ */ int -drm_gem_init (struct drm_device *dev) +drm_gem_init(struct drm_device *dev) { - spin_lock_init (&dev->object_name_lock); - idr_init (&dev->object_name_idr); - atomic_set (&dev->object_count, 0); + spin_lock_init(&dev->object_name_lock); + idr_init(&dev->object_name_idr); + atomic_set(&dev->object_count, 0); return 0; } @@ -96,8 +96,8 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) return NULL; } - kref_init (&obj->refcount); - kref_init (&obj->handlecount); + kref_init(&obj->refcount); + kref_init(&obj->handlecount); obj->size = size; if (dev->driver->gem_init_object != NULL && @@ -106,7 +106,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) kfree(obj); return NULL; } - atomic_inc (&dev->object_count); + atomic_inc(&dev->object_count); return obj; } @@ -151,7 +151,7 @@ drm_gem_handle_delete(struct drm_file *filp, int handle) * will likely want to dereference the object afterwards. */ static int -drm_gem_handle_create (struct drm_file *file_priv, +drm_gem_handle_create(struct drm_file *file_priv, struct drm_gem_object *obj, int *handlep) { @@ -166,16 +166,16 @@ again: return -ENOMEM; /* do the allocation under our spinlock */ - spin_lock (&file_priv->table_lock); + spin_lock(&file_priv->table_lock); ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep); - spin_unlock (&file_priv->table_lock); + spin_unlock(&file_priv->table_lock); if (ret == -EAGAIN) goto again; if (ret != 0) return ret; - - drm_gem_object_handle_reference (obj); + + drm_gem_object_handle_reference(obj); return 0; } @@ -224,7 +224,7 @@ drm_gem_alloc_ioctl(struct drm_device *dev, void *data, if (obj == NULL) return -ENOMEM; - ret = drm_gem_handle_create (file_priv, obj, &handle); + ret = drm_gem_handle_create(file_priv, obj, &handle); drm_gem_object_handle_unreference(obj); if (ret) @@ -324,7 +324,7 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, drm_gem_object_unreference(obj); if (IS_ERR((void *)addr)) return addr; - + args->addr_ptr = (uint64_t) addr; return 0; @@ -391,17 +391,17 @@ drm_gem_name_ioctl(struct drm_device *dev, void *data, return -EINVAL; again: - if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) { + if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) return -ENOMEM; - } + spin_lock(&dev->object_name_lock); if (obj->name) { - spin_unlock (&dev->object_name_lock); + spin_unlock(&dev->object_name_lock); return -EEXIST; } - ret = idr_get_new_above (&dev->object_name_idr, obj, 1, + ret = idr_get_new_above(&dev->object_name_idr, obj, 1, &obj->name); - spin_unlock (&dev->object_name_lock); + spin_unlock(&dev->object_name_lock); if (ret == -EAGAIN) goto again; @@ -410,7 +410,7 @@ again: return ret; } - /* + /* * Leave the reference from the lookup around as the * name table now holds one */ @@ -437,16 +437,16 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data, if (!(dev->driver->driver_features & DRIVER_GEM)) return -ENODEV; - spin_lock (&dev->object_name_lock); - obj = idr_find (&dev->object_name_idr, (int) args->name); + spin_lock(&dev->object_name_lock); + obj = idr_find(&dev->object_name_idr, (int) args->name); if (obj) - drm_gem_object_reference (obj); - spin_unlock (&dev->object_name_lock); + drm_gem_object_reference(obj); + spin_unlock(&dev->object_name_lock); if (!obj) return -ENOENT; - ret = drm_gem_handle_create (file_priv, obj, &handle); - drm_gem_object_unreference (obj); + ret = drm_gem_handle_create(file_priv, obj, &handle); + drm_gem_object_unreference(obj); if (ret) return ret; @@ -460,7 +460,7 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data, * Called when user space prepares to use an object */ int -drm_gem_set_domain_ioctl (struct drm_device *dev, void *data, +drm_gem_set_domain_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { struct drm_gem_set_domain *args = data; @@ -475,7 +475,7 @@ drm_gem_set_domain_ioctl (struct drm_device *dev, void *data, return -EINVAL; if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain (obj, + ret = dev->driver->gem_set_domain(obj, args->read_domains, args->write_domain); } else { @@ -483,7 +483,7 @@ drm_gem_set_domain_ioctl (struct drm_device *dev, void *data, obj->write_domain = args->write_domain; ret = 0; } - drm_gem_object_unreference (obj); + drm_gem_object_unreference(obj); return ret; } @@ -495,12 +495,15 @@ void drm_gem_open(struct drm_device *dev, struct drm_file *file_private) { idr_init(&file_private->object_idr); - spin_lock_init (&file_private->table_lock); + spin_lock_init(&file_private->table_lock); } -/** Called at device close to release the file's handle references on objects. */ +/** + * Called at device close to release the file's + * handle references on objects. + */ static int -drm_gem_object_release_handle (int id, void *ptr, void *data) +drm_gem_object_release_handle(int id, void *ptr, void *data) { struct drm_gem_object *obj = ptr; @@ -517,7 +520,8 @@ drm_gem_object_release_handle (int id, void *ptr, void *data) void drm_gem_release(struct drm_device *dev, struct drm_file *file_private) { - idr_for_each(&file_private->object_idr, &drm_gem_object_release_handle, NULL); + idr_for_each(&file_private->object_idr, + &drm_gem_object_release_handle, NULL); idr_destroy(&file_private->object_idr); } @@ -528,7 +532,7 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private) * Frees the object */ void -drm_gem_object_free (struct kref *kref) +drm_gem_object_free(struct kref *kref) { struct drm_gem_object *obj = (struct drm_gem_object *) kref; struct drm_device *dev = obj->dev; @@ -537,7 +541,7 @@ drm_gem_object_free (struct kref *kref) dev->driver->gem_free_object(obj); fput(obj->filp); - atomic_dec (&dev->object_count); + atomic_dec(&dev->object_count); kfree(obj); } EXPORT_SYMBOL(drm_gem_object_free); @@ -550,24 +554,26 @@ EXPORT_SYMBOL(drm_gem_object_free); * freed memory */ void -drm_gem_object_handle_free (struct kref *kref) +drm_gem_object_handle_free(struct kref *kref) { - struct drm_gem_object *obj = container_of (kref, struct drm_gem_object, handlecount); + struct drm_gem_object *obj = container_of(kref, + struct drm_gem_object, + handlecount); struct drm_device *dev = obj->dev; /* Remove any name for this object */ - spin_lock (&dev->object_name_lock); + spin_lock(&dev->object_name_lock); if (obj->name) { - idr_remove (&dev->object_name_idr, obj->name); - spin_unlock (&dev->object_name_lock); + idr_remove(&dev->object_name_idr, obj->name); + spin_unlock(&dev->object_name_lock); /* * The object name held a reference to this object, drop * that now. */ - drm_gem_object_unreference (obj); + drm_gem_object_unreference(obj); } else - spin_unlock (&dev->object_name_lock); - + spin_unlock(&dev->object_name_lock); + } EXPORT_SYMBOL(drm_gem_object_handle_free); diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 5c58032b..d25c99a9 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -883,11 +883,13 @@ err: drm_gem_object_unreference(object_list[i]); } +#if 0 /* XXX kludge for now as we don't clean the exec ring yet */ if (object_list != NULL) { for (i = 0; i < args->buffer_count; i++) i915_gem_object_wait_rendering (object_list[i]); } +#endif drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); -- cgit v1.2.3 From ec75369b402235d74b06b08907572050962075a6 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 8 May 2008 11:45:53 -0700 Subject: [i915] clean up whinging from checkpatch.pl --- linux-core/i915_gem.c | 410 +++++++++++++++++++++++++++----------------------- 1 file changed, 218 insertions(+), 192 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index d25c99a9..ff8e2762 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -63,12 +63,11 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) if (obj_priv->page_list == NULL) return; - - for (i = 0; i < page_count; i++) { - if (obj_priv->page_list[i] != NULL) { - page_cache_release (obj_priv->page_list[i]); - } - } + + for (i = 0; i < page_count; i++) + if (obj_priv->page_list[i] != NULL) + page_cache_release(obj_priv->page_list[i]); + drm_free(obj_priv->page_list, page_count * sizeof(struct page *), DRM_MEM_DRIVER); @@ -76,56 +75,66 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) } static void -i915_gem_flush(struct drm_device *dev, uint32_t invalidate_domains, uint32_t flush_domains) +i915_gem_flush(struct drm_device *dev, + uint32_t invalidate_domains, + uint32_t flush_domains) { drm_i915_private_t *dev_priv = dev->dev_private; - uint32_t cmd; + uint32_t cmd; RING_LOCALS; #if WATCH_EXEC - DRM_INFO ("%s: invalidate %08x flush %08x\n", __FUNCTION__, + DRM_INFO("%s: invalidate %08x flush %08x\n", __func__, invalidate_domains, flush_domains); #endif if (flush_domains & DRM_GEM_DOMAIN_CPU) drm_agp_chipset_flush(dev); - - if ((invalidate_domains|flush_domains) & ~DRM_GEM_DOMAIN_CPU) - { - /* read/write caches: + + if ((invalidate_domains|flush_domains) & ~DRM_GEM_DOMAIN_CPU) { + /* + * read/write caches: + * * DRM_GEM_DOMAIN_I915_RENDER is always invalidated, but is - * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is also - * flushed at 2d versus 3d pipeline switches. + * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is + * also flushed at 2d versus 3d pipeline switches. * * read-only caches: - * DRM_GEM_DOMAIN_I915_SAMPLER is flushed on pre-965 if MI_READ_FLUSH - * is set, and is always flushed on 965. + * + * DRM_GEM_DOMAIN_I915_SAMPLER is flushed on pre-965 if + * MI_READ_FLUSH is set, and is always flushed on 965. + * * DRM_GEM_DOMAIN_I915_COMMAND may not exist? - * DRM_GEM_DOMAIN_I915_INSTRUCTION, which exists on 965, is invalidated - * when MI_EXE_FLUSH is set. - * DRM_GEM_DOMAIN_I915_VERTEX, which exists on 965, is invalidated with - * every MI_FLUSH. + * + * DRM_GEM_DOMAIN_I915_INSTRUCTION, which exists on 965, is + * invalidated when MI_EXE_FLUSH is set. + * + * DRM_GEM_DOMAIN_I915_VERTEX, which exists on 965, is + * invalidated with every MI_FLUSH. * * TLBs: - * On 965, TLBs associated with DRM_GEM_DOMAIN_I915_COMMAND and - * DRM_GEM_DOMAIN_CPU in are invalidated at PTE write and - * DRM_GEM_DOMAIN_I915_RENDER and DRM_GEM_DOMAIN_I915_SAMPLER are - * flushed at any MI_FLUSH. + * + * On 965, TLBs associated with DRM_GEM_DOMAIN_I915_COMMAND + * and DRM_GEM_DOMAIN_CPU in are invalidated at PTE write and + * DRM_GEM_DOMAIN_I915_RENDER and DRM_GEM_DOMAIN_I915_SAMPLER + * are flushed at any MI_FLUSH. */ - + cmd = CMD_MI_FLUSH | MI_NO_WRITE_FLUSH; - if ((invalidate_domains|flush_domains) & DRM_GEM_DOMAIN_I915_RENDER) + if ((invalidate_domains|flush_domains) & + DRM_GEM_DOMAIN_I915_RENDER) cmd &= ~MI_NO_WRITE_FLUSH; if (!IS_I965G(dev)) { - /* On the 965, the sampler cache always gets flushed and this - * bit is reserved. + /* + * On the 965, the sampler cache always gets flushed + * and this bit is reserved. */ if (invalidate_domains & DRM_GEM_DOMAIN_I915_SAMPLER) cmd |= MI_READ_FLUSH; } if (invalidate_domains & DRM_GEM_DOMAIN_I915_INSTRUCTION) cmd |= MI_EXE_FLUSH; - + BEGIN_LP_RING(2); OUT_RING(cmd); OUT_RING(0); /* noop */ @@ -147,34 +156,36 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) /* If there are writes queued to the buffer, flush and * create a new cookie to wait for. */ - if (obj->write_domain & ~(DRM_GEM_DOMAIN_CPU)) - { + if (obj->write_domain & ~(DRM_GEM_DOMAIN_CPU)) { #if WATCH_BUF - DRM_INFO ("%s: flushing object %p from write domain %08x\n", - __FUNCTION__, obj, obj->write_domain); + DRM_INFO("%s: flushing object %p from write domain %08x\n", + __func__, obj, obj->write_domain); #endif - i915_gem_flush (dev, 0, obj->write_domain); + i915_gem_flush(dev, 0, obj->write_domain); obj->write_domain = 0; if (obj_priv->last_rendering_cookie == 0) - drm_gem_object_reference (obj); - obj_priv->last_rendering_cookie = i915_emit_irq (dev); + drm_gem_object_reference(obj); + obj_priv->last_rendering_cookie = i915_emit_irq(dev); } /* If there is rendering queued on the buffer being evicted, wait for * it. */ if (obj_priv->last_rendering_cookie != 0) { #if WATCH_BUF - DRM_INFO ("%s: object %p wait for cookie %08x\n", - __FUNCTION__, obj, obj_priv->last_rendering_cookie); + DRM_INFO("%s: object %p wait for cookie %08x\n", + __func__, obj, obj_priv->last_rendering_cookie); #endif ret = i915_wait_irq(dev, obj_priv->last_rendering_cookie); if (ret != 0) return ret; /* Clear it now that we know it's passed. */ obj_priv->last_rendering_cookie = 0; - - /* The cookie held a reference to the object, release that now */ - drm_gem_object_unreference (obj); + + /* + * The cookie held a reference to the object, + * release that now + */ + drm_gem_object_unreference(obj); } return 0; @@ -189,8 +200,8 @@ i915_gem_object_unbind(struct drm_gem_object *obj) struct drm_i915_gem_object *obj_priv = obj->driver_private; #if WATCH_BUF - DRM_INFO ("%s:%d %p\n", __FUNCTION__, __LINE__, obj); - DRM_INFO ("gtt_space %p\n", obj_priv->gtt_space); + DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj); + DRM_INFO("gtt_space %p\n", obj_priv->gtt_space); #endif if (obj_priv->gtt_space == NULL) return; @@ -206,34 +217,35 @@ i915_gem_object_unbind(struct drm_gem_object *obj) drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; - if (!list_empty (&obj_priv->gtt_lru_entry)) + if (!list_empty(&obj_priv->gtt_lru_entry)) list_del_init(&obj_priv->gtt_lru_entry); } #if WATCH_BUF | WATCH_EXEC static void -i915_gem_dump_page (struct page *page, uint32_t start, uint32_t end, uint32_t bias, uint32_t mark) +i915_gem_dump_page(struct page *page, uint32_t start, uint32_t end, + uint32_t bias, uint32_t mark) { - uint32_t *mem = kmap_atomic (page, KM_USER0); + uint32_t *mem = kmap_atomic(page, KM_USER0); int i; for (i = start; i < end; i += 4) - DRM_INFO ("%08x: %08x%s\n", + DRM_INFO("%08x: %08x%s\n", (int) (bias + i), mem[i / 4], (bias + i == mark) ? " ********" : ""); - kunmap_atomic (mem, KM_USER0); + kunmap_atomic(mem, KM_USER0); /* give syslog time to catch up */ - msleep (1); + msleep(1); } static void -i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where, uint32_t mark) +i915_gem_dump_object(struct drm_gem_object *obj, int len, + const char *where, uint32_t mark) { struct drm_i915_gem_object *obj_priv = obj->driver_private; int page; - DRM_INFO ("%s: object at offset %08x\n", where, obj_priv->gtt_offset); - for (page = 0; page < (len + PAGE_SIZE-1) / PAGE_SIZE; page++) - { + DRM_INFO("%s: object at offset %08x\n", where, obj_priv->gtt_offset); + for (page = 0; page < (len + PAGE_SIZE-1) / PAGE_SIZE; page++) { int page_len, chunk, chunk_len; page_len = len - page * PAGE_SIZE; @@ -244,10 +256,11 @@ i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where, ui chunk_len = page_len - chunk; if (chunk_len > 128) chunk_len = 128; - i915_gem_dump_page (obj_priv->page_list[page], - chunk, chunk + chunk_len, - obj_priv->gtt_offset + page * PAGE_SIZE, - mark); + i915_gem_dump_page(obj_priv->page_list[page], + chunk, chunk + chunk_len, + obj_priv->gtt_offset + + page * PAGE_SIZE, + mark); } } } @@ -255,16 +268,17 @@ i915_gem_dump_object (struct drm_gem_object *obj, int len, const char *where, ui #if WATCH_LRU static void -i915_dump_lru (struct drm_device *dev, const char *where) +i915_dump_lru(struct drm_device *dev, const char *where) { drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv; - - DRM_INFO ("GTT LRU %s {\n", where); - list_for_each_entry (obj_priv, &dev_priv->mm.gtt_lru, gtt_lru_entry) { - DRM_INFO (" %p: %08x\n", obj_priv, obj_priv->last_rendering_cookie); + + DRM_INFO("GTT LRU %s {\n", where); + list_for_each_entry(obj_priv, &dev_priv->mm.gtt_lru, gtt_lru_entry) { + DRM_INFO(" %p: %08x\n", obj_priv, + obj_priv->last_rendering_cookie); } - DRM_INFO ("}\n"); + DRM_INFO("}\n"); } #endif @@ -283,7 +297,7 @@ i915_gem_evict_something(struct drm_device *dev) gtt_lru_entry); obj = obj_priv->obj; #if WATCH_LRU - DRM_INFO ("%s: evicting %p\n", __FUNCTION__, obj); + DRM_INFO("%s: evicting %p\n", __func__, obj); #endif /* Only unpinned buffers should be on this list. */ @@ -300,7 +314,7 @@ i915_gem_evict_something(struct drm_device *dev) /* Wait on the rendering and unbind the buffer. */ i915_gem_object_unbind(obj); #if WATCH_LRU - DRM_INFO ("%s: evicted %p\n", __FUNCTION__, obj); + DRM_INFO("%s: evicted %p\n", __func__, obj); #endif return 0; @@ -343,10 +357,10 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) * fitting our object in, we're out of memory. */ #if WATCH_LRU - DRM_INFO ("%s: GTT full, evicting something\n", __FUNCTION__); + DRM_INFO("%s: GTT full, evicting something\n", __func__); #endif if (list_empty(&dev_priv->mm.gtt_lru)) { - DRM_ERROR ("GTT full, but LRU list empty\n"); + DRM_ERROR("GTT full, but LRU list empty\n"); return -ENOMEM; } @@ -357,7 +371,8 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) } #if WATCH_BUF - DRM_INFO ("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); + DRM_INFO("Binding object of size %d at 0x%08x\n", + obj->size, obj_priv->gtt_offset); #endif /* Get the list of pages out of our struct file. They'll be pinned @@ -383,9 +398,9 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) obj_priv->gtt_space = NULL; return -ENOMEM; } - unlock_page (obj_priv->page_list[i]); + unlock_page(obj_priv->page_list[i]); } - + /* Create an AGP memory structure pointing at our pages, and bind it * into the GTT. */ @@ -411,7 +426,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) } static void -i915_gem_clflush_object (struct drm_gem_object *obj) +i915_gem_clflush_object(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -422,26 +437,26 @@ i915_gem_clflush_object (struct drm_gem_object *obj) if (obj_priv->page_list == NULL) return; - drm_ttm_cache_flush (obj_priv->page_list, obj->size / PAGE_SIZE); + drm_ttm_cache_flush(obj_priv->page_list, obj->size / PAGE_SIZE); } - + /* * Set the next domain for the specified object. This * may not actually perform the necessary flushing/invaliding though, * as that may want to be batched with other set_domain operations */ static void -i915_gem_object_set_domain (struct drm_gem_object *obj, +i915_gem_object_set_domain(struct drm_gem_object *obj, uint32_t read_domains, uint32_t write_domain) { struct drm_device *dev = obj->dev; uint32_t invalidate_domains = 0; uint32_t flush_domains = 0; - + #if WATCH_BUF - DRM_INFO ("%s: object %p read %08x write %08x\n", - __FUNCTION__, obj, read_domains, write_domain); + DRM_INFO("%s: object %p read %08x write %08x\n", + __func__, obj, read_domains, write_domain); #endif /* * Flush the current write domain if @@ -449,8 +464,7 @@ i915_gem_object_set_domain (struct drm_gem_object *obj, * any read domains which differ from the old * write domain */ - if (obj->write_domain && obj->write_domain != read_domains) - { + if (obj->write_domain && obj->write_domain != read_domains) { flush_domains |= obj->write_domain; invalidate_domains |= read_domains & ~obj->write_domain; } @@ -459,21 +473,20 @@ i915_gem_object_set_domain (struct drm_gem_object *obj, * stale data. That is, any new read domains. */ invalidate_domains |= read_domains & ~obj->read_domains; - if ((flush_domains | invalidate_domains) & DRM_GEM_DOMAIN_CPU) - { + if ((flush_domains | invalidate_domains) & DRM_GEM_DOMAIN_CPU) { #if WATCH_BUF - DRM_INFO ("%s: CPU domain flush %08x invalidate %08x\n", - __FUNCTION__, flush_domains, invalidate_domains); + DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n", + __func__, flush_domains, invalidate_domains); #endif /* * If we're invaliding the CPU cache and flushing a GPU cache, - * then pause for rendering so that the GPU caches will be + * then pause for rendering so that the GPU caches will be * flushed before the cpu cache is invalidated */ if ((invalidate_domains & DRM_GEM_DOMAIN_CPU) && (flush_domains & ~DRM_GEM_DOMAIN_CPU)) - i915_gem_object_wait_rendering (obj); - i915_gem_clflush_object (obj); + i915_gem_object_wait_rendering(obj); + i915_gem_clflush_object(obj); } obj->write_domain = write_domain; @@ -488,19 +501,22 @@ i915_gem_object_set_domain (struct drm_gem_object *obj, */ static void -i915_gem_dev_set_domain (struct drm_device *dev) +i915_gem_dev_set_domain(struct drm_device *dev) { /* * Now that all the buffers are synced to the proper domains, * flush and invalidate the collected domains */ - if (dev->invalidate_domains | dev->flush_domains) - { + if (dev->invalidate_domains | dev->flush_domains) { #if WATCH_EXEC - DRM_INFO ("%s: invalidate_domains %08x flush_domains %08x\n", - __FUNCTION__, dev->invalidate_domains, dev->flush_domains); + DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n", + __func__, + dev->invalidate_domains, + dev->flush_domains); #endif - i915_gem_flush (dev, dev->invalidate_domains, dev->flush_domains); + i915_gem_flush(dev, + dev->invalidate_domains, + dev->flush_domains); dev->invalidate_domains = 0; dev->flush_domains = 0; } @@ -531,16 +547,19 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, if (obj_priv->pin_count == 0) { /* Move our buffer to the head of the LRU. */ - if (list_empty (&obj_priv->gtt_lru_entry)) - list_add_tail(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); + if (list_empty(&obj_priv->gtt_lru_entry)) + list_add_tail(&obj_priv->gtt_lru_entry, + &dev_priv->mm.gtt_lru); else - list_move_tail(&obj_priv->gtt_lru_entry, &dev_priv->mm.gtt_lru); + list_move_tail(&obj_priv->gtt_lru_entry, + &dev_priv->mm.gtt_lru); #if WATCH_LRU && 0 - i915_dump_lru (dev, __FUNCTION__); + i915_dump_lru(dev, __func__); #endif } - relocs = (struct drm_i915_gem_relocation_entry __user *) (uintptr_t) entry->relocs_ptr; + relocs = (struct drm_i915_gem_relocation_entry __user *) + (uintptr_t) entry->relocs_ptr; /* Apply the relocations, using the GTT aperture to avoid cache * flushing requirements. */ @@ -566,46 +585,55 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, if (target_obj_priv->gtt_space == NULL) { DRM_ERROR("No GTT space found for object %d\n", reloc.target_handle); - drm_gem_object_unreference (target_obj); + drm_gem_object_unreference(target_obj); return -EINVAL; } if (reloc.offset > obj->size - 4) { - DRM_ERROR("Relocation beyond object bounds: obj %p target %d offset %d size %d.\n", - obj, reloc.target_handle, (int) reloc.offset, (int) obj->size); - drm_gem_object_unreference (target_obj); + DRM_ERROR("Relocation beyond object bounds: " + "obj %p target %d offset %d size %d.\n", + obj, reloc.target_handle, + (int) reloc.offset, (int) obj->size); + drm_gem_object_unreference(target_obj); return -EINVAL; } if (reloc.offset & 3) { - DRM_ERROR("Relocation not 4-byte aligned: obj %p target %d offset %d.\n", - obj, reloc.target_handle, (int) reloc.offset); - drm_gem_object_unreference (target_obj); + DRM_ERROR("Relocation not 4-byte aligned: " + "obj %p target %d offset %d.\n", + obj, reloc.target_handle, + (int) reloc.offset); + drm_gem_object_unreference(target_obj); return -EINVAL; } if (reloc.write_domain && target_obj->pending_write_domain && - reloc.write_domain != target_obj->pending_write_domain) - { - DRM_ERROR("Write domain conflict: obj %p target %d offset %d new %08x old %08x\n", - obj, reloc.target_handle, (int) reloc.offset, - reloc.write_domain, target_obj->pending_write_domain); - drm_gem_object_unreference (target_obj); + reloc.write_domain != target_obj->pending_write_domain) { + DRM_ERROR("Write domain conflict: " + "obj %p target %d offset %d " + "new %08x old %08x\n", + obj, reloc.target_handle, + (int) reloc.offset, + reloc.write_domain, + target_obj->pending_write_domain); + drm_gem_object_unreference(target_obj); return -EINVAL; } - + #if WATCH_RELOC - DRM_INFO ("%s: obj %p offset %08x target %d read %08x write %08x gtt %08x presumed %08x delta %08x\n", - __FUNCTION__, - obj, - (int) reloc.offset, - (int) reloc.target_handle, - (int) reloc.read_domains, - (int) reloc.write_domain, - (int) target_obj_priv->gtt_offset, - (int) reloc.presumed_offset, - reloc.delta); + DRM_INFO("%s: obj %p offset %08x target %d " + "read %08x write %08x gtt %08x " + "presumed %08x delta %08x\n", + __func__, + obj, + (int) reloc.offset, + (int) reloc.target_handle, + (int) reloc.read_domains, + (int) reloc.write_domain, + (int) target_obj_priv->gtt_offset, + (int) reloc.presumed_offset, + reloc.delta); #endif - + target_obj->pending_read_domains |= reloc.read_domains; target_obj->pending_write_domain |= reloc.write_domain; @@ -624,9 +652,8 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, /* As we're writing through the gtt, flush * any CPU writes before we write the relocations */ - if (obj->write_domain & DRM_GEM_DOMAIN_CPU) - { - i915_gem_clflush_object (obj); + if (obj->write_domain & DRM_GEM_DOMAIN_CPU) { + i915_gem_clflush_object(obj); drm_agp_chipset_flush(dev); obj->write_domain = 0; } @@ -646,7 +673,7 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, PAGE_SIZE); last_reloc_offset = reloc_offset; if (reloc_page == NULL) { - drm_gem_object_unreference (target_obj); + drm_gem_object_unreference(target_obj); return -ENOMEM; } } @@ -658,29 +685,30 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, #if WATCH_BUF DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n", obj, (unsigned int) reloc.offset, - readl (reloc_entry), reloc_val); + readl(reloc_entry), reloc_val); #endif - writel (reloc_val, reloc_entry); + writel(reloc_val, reloc_entry); - drm_gem_object_unreference (target_obj); + drm_gem_object_unreference(target_obj); } if (reloc_page != NULL) iounmap(reloc_page); #if WATCH_BUF - i915_gem_dump_object (obj, 128, __FUNCTION__, ~0); + i915_gem_dump_object(obj, 128, __func__, ~0); #endif return 0; } static int -i915_dispatch_gem_execbuffer (struct drm_device * dev, - struct drm_i915_gem_execbuffer * exec, +i915_dispatch_gem_execbuffer(struct drm_device *dev, + struct drm_i915_gem_execbuffer *exec, uint64_t exec_offset) { drm_i915_private_t *dev_priv = dev->dev_private; - struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *) (uintptr_t) exec->cliprects_ptr; + struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *) + (uintptr_t) exec->cliprects_ptr; int nbox = exec->num_cliprects; int i = 0, count; uint32_t exec_start, exec_len; @@ -688,7 +716,7 @@ i915_dispatch_gem_execbuffer (struct drm_device * dev, exec_start = (uint32_t) exec_offset + exec->batch_start_offset; exec_len = (uint32_t) exec->batch_len; - + if ((exec_start | exec_len) & 0x7) { DRM_ERROR("alignment\n"); return -EINVAL; @@ -710,10 +738,13 @@ i915_dispatch_gem_execbuffer (struct drm_device * dev, if (dev_priv->use_mi_batchbuffer_start) { BEGIN_LP_RING(2); if (IS_I965G(dev)) { - OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965); + OUT_RING(MI_BATCH_BUFFER_START | + (2 << 6) | + MI_BATCH_NON_SECURE_I965); OUT_RING(exec_start); } else { - OUT_RING(MI_BATCH_BUFFER_START | (2 << 6)); + OUT_RING(MI_BATCH_BUFFER_START | + (2 << 6)); OUT_RING(exec_start | MI_BATCH_NON_SECURE); } ADVANCE_LP_RING(); @@ -739,27 +770,15 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_i915_gem_execbuffer *args = data; struct drm_i915_gem_validate_entry *validate_list; struct drm_gem_object **object_list; + struct drm_gem_object *batch_obj; int ret, i; uint64_t exec_offset; uint32_t cookie; LOCK_TEST_WITH_RETURN(dev, file_priv); -#if 0 - /* - * XXX wait for previous rendering to complete as we otherwise never - * flush the LRU list - */ - { - drm_i915_private_t *dev_priv = dev->dev_private; - - while (!list_empty (&dev_priv->mm.gtt_lru)) - i915_gem_evict_something (dev); - } -#endif - #if WATCH_EXEC - DRM_INFO ("buffers_ptr %d buffer_count %d len %08x\n", + DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", (int) args->buffers_ptr, args->buffer_count, args->batch_len); #endif i915_kernel_lost_context(dev); @@ -770,42 +789,47 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, object_list = drm_calloc(sizeof(*object_list), args->buffer_count, DRM_MEM_DRIVER); if (validate_list == NULL || object_list == NULL) { - DRM_ERROR ("Failed to allocate validate or object list for %d buffers\n", - args->buffer_count); + DRM_ERROR("Failed to allocate validate or object list " + "for %d buffers\n", + args->buffer_count); ret = -ENOMEM; goto err; } ret = copy_from_user(validate_list, - (struct drm_i915_relocation_entry __user*)(uintptr_t) - args->buffers_ptr, + (struct drm_i915_relocation_entry __user *) + (uintptr_t) args->buffers_ptr, sizeof(*validate_list) * args->buffer_count); if (ret != 0) { - DRM_ERROR ("copy %d validate entries failed %d\n", args->buffer_count, ret); + DRM_ERROR("copy %d validate entries failed %d\n", + args->buffer_count, ret); goto err; } /* Look up object handles and perform the relocations */ for (i = 0; i < args->buffer_count; i++) { object_list[i] = drm_gem_object_lookup(dev, file_priv, - validate_list[i].buffer_handle); + validate_list[i]. + buffer_handle); if (object_list[i] == NULL) { - DRM_ERROR ("Invalid object handle %d at index %d\n", + DRM_ERROR("Invalid object handle %d at index %d\n", validate_list[i].buffer_handle, i); ret = -EINVAL; goto err; } - ret = i915_gem_reloc_and_validate_object(object_list[i], file_priv, + ret = i915_gem_reloc_and_validate_object(object_list[i], + file_priv, &validate_list[i]); if (ret) { - DRM_ERROR ("reloc and validate failed %d\n", ret); + DRM_ERROR("reloc and validate failed %d\n", ret); goto err; } } /* Set the pending read domains for the batch buffer to COMMAND */ - object_list[args->buffer_count-1]->pending_read_domains = DRM_GEM_DOMAIN_I915_COMMAND; - object_list[args->buffer_count-1]->pending_write_domain = 0; + batch_obj = object_list[args->buffer_count-1]; + batch_obj->pending_read_domains = DRM_GEM_DOMAIN_I915_COMMAND; + batch_obj->pending_write_domain = 0; for (i = 0; i < args->buffer_count; i++) { struct drm_gem_object *obj = object_list[i]; @@ -822,7 +846,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } /* make sure all previous memory operations have passed */ - i915_gem_object_set_domain (obj, + i915_gem_object_set_domain(obj, obj->pending_read_domains, obj->pending_write_domain); obj->pending_read_domains = 0; @@ -830,30 +854,30 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } /* Flush/invalidate caches and chipset buffer */ - i915_gem_dev_set_domain (dev); + i915_gem_dev_set_domain(dev); exec_offset = validate_list[args->buffer_count - 1].buffer_offset; #if WATCH_EXEC - i915_gem_dump_object (object_list[args->buffer_count - 1], + i915_gem_dump_object(object_list[args->buffer_count - 1], args->batch_len, - __FUNCTION__, + __func__, ~0); #endif - + /* Exec the batchbuffer */ - ret = i915_dispatch_gem_execbuffer (dev, args, exec_offset); - if (ret) - { - DRM_ERROR ("dispatch failed %d\n", ret); + ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset); + if (ret) { + DRM_ERROR("dispatch failed %d\n", ret); goto err; } - /* Get a cookie representing the execution of the current buffer, which we - * can wait on. We would like to mitigate these interrupts, likely by - * only creating cookies occasionally (so that we have *some* interrupts - * representing completion of buffers that we can wait on when trying - * to clear up gtt space). + /* + * Get a cookie representing the execution of the current buffer, + * which we can wait on. We would like to mitigate these interrupts, + * likely by only creating cookies occasionally (so that we have + * *some* interrupts representing completion of buffers that we can + * wait on when trying to clear up gtt space). */ cookie = i915_emit_irq(dev); for (i = 0; i < args->buffer_count; i++) { @@ -865,17 +889,18 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, * which is freed when the object is waited for */ if (obj_priv->last_rendering_cookie == 0) - drm_gem_object_reference (obj); + drm_gem_object_reference(obj); obj_priv->last_rendering_cookie = cookie; } /* Copy the new buffer offsets back to the user's validate list. */ - ret = copy_to_user((struct drm_i915_relocation_entry __user*)(uintptr_t) - args->buffers_ptr, + ret = copy_to_user((struct drm_i915_relocation_entry __user *) + (uintptr_t) args->buffers_ptr, validate_list, sizeof(*validate_list) * args->buffer_count); if (ret) - DRM_ERROR ("failed to copy %d validate entries back to user (%d)\n", + DRM_ERROR("failed to copy %d validate entries " + "back to user (%d)\n", args->buffer_count, ret); err: if (object_list != NULL) { @@ -883,11 +908,11 @@ err: drm_gem_object_unreference(object_list[i]); } -#if 0 +#if 1 /* XXX kludge for now as we don't clean the exec ring yet */ if (object_list != NULL) { for (i = 0; i < args->buffer_count; i++) - i915_gem_object_wait_rendering (object_list[i]); + i915_gem_object_wait_rendering(object_list[i]); } #endif @@ -916,20 +941,21 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, } obj_priv = obj->driver_private; - if (obj_priv->gtt_space == NULL) - { - ret = i915_gem_object_bind_to_gtt(obj, (unsigned) args->alignment); + if (obj_priv->gtt_space == NULL) { + ret = i915_gem_object_bind_to_gtt(obj, + (unsigned) args->alignment); if (ret != 0) { - DRM_ERROR("Failure to bind in i915_gem_pin_ioctl(): %d\n", + DRM_ERROR("Failure to bind in " + "i915_gem_pin_ioctl(): %d\n", ret); - drm_gem_object_unreference (obj); + drm_gem_object_unreference(obj); return ret; } } obj_priv->pin_count++; args->offset = obj_priv->gtt_offset; - drm_gem_object_unreference (obj); + drm_gem_object_unreference(obj); return 0; } @@ -951,7 +977,7 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, obj_priv = obj->driver_private; obj_priv->pin_count--; - drm_gem_object_unreference (obj); + drm_gem_object_unreference(obj); return 0; } @@ -977,11 +1003,11 @@ void i915_gem_free_object(struct drm_gem_object *obj) } int -i915_gem_set_domain_ioctl (struct drm_gem_object *obj, +i915_gem_set_domain_ioctl(struct drm_gem_object *obj, uint32_t read_domains, uint32_t write_domain) { - i915_gem_object_set_domain (obj, read_domains, write_domain); - i915_gem_dev_set_domain (obj->dev); + i915_gem_object_set_domain(obj, read_domains, write_domain); + i915_gem_dev_set_domain(obj->dev); return 0; } -- cgit v1.2.3 From 1e26ca44c9f3e8a1a30652aa860b405e0248aae1 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 9 May 2008 12:18:09 -0700 Subject: [gem] API cleanup. allocate->create unreference->close name->flink Make the API names a bit more consistent. --- linux-core/drmP.h | 10 +++++----- linux-core/drm_drv.c | 6 +++--- linux-core/drm_gem.c | 20 ++++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 11688cdd..419b4be3 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -1394,18 +1394,18 @@ static inline void drm_gem_object_handle_unreference (struct drm_gem_object *obj struct drm_gem_object * drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, int handle); -int drm_gem_alloc_ioctl(struct drm_device *dev, void *data, +int drm_gem_create_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int drm_gem_close_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); -int drm_gem_unreference_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); int drm_gem_pread_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int drm_gem_mmap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); -int drm_gem_name_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); +int drm_gem_flink_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); int drm_gem_open_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int drm_gem_set_domain_ioctl(struct drm_device *dev, void *data, diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index 16c38dbe..edc1f057 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -151,12 +151,12 @@ static struct drm_ioctl_desc drm_ioctls[] = { DRM_IOCTL_DEF(DRM_IOCTL_MM_INFO, drm_mm_info_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_GEM_ALLOC, drm_gem_alloc_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_GEM_UNREFERENCE, drm_gem_unreference_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_CREATE, drm_gem_create_ioctl, 0), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_GEM_PREAD, drm_gem_pread_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_GEM_PWRITE, drm_gem_pwrite_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_GEM_MMAP, drm_gem_mmap_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_GEM_NAME, drm_gem_name_ioctl, DRM_AUTH), + DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH), DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH), DRM_IOCTL_DEF(DRM_IOCTL_GEM_SET_DOMAIN, drm_gem_set_domain_ioctl, DRM_AUTH), }; diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index c85265ad..6c462921 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -204,13 +204,13 @@ drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, EXPORT_SYMBOL(drm_gem_object_lookup); /** - * Allocates a new mm object and returns a handle to it. + * Creates a new mm object and returns a handle to it. */ int -drm_gem_alloc_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) +drm_gem_create_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) { - struct drm_gem_alloc *args = data; + struct drm_gem_create *args = data; struct drm_gem_object *obj; int handle, ret; @@ -239,10 +239,10 @@ drm_gem_alloc_ioctl(struct drm_device *dev, void *data, * Releases the handle to an mm object. */ int -drm_gem_unreference_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) +drm_gem_close_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) { - struct drm_gem_unreference *args = data; + struct drm_gem_close *args = data; int ret; if (!(dev->driver->driver_features & DRIVER_GEM)) @@ -376,10 +376,10 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, * is freed, the name goes away. */ int -drm_gem_name_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) +drm_gem_flink_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) { - struct drm_gem_name *args = data; + struct drm_gem_flink *args = data; struct drm_gem_object *obj; int ret; -- cgit v1.2.3 From f0ae335cd70077043f2f7af39d7edcc529367c61 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 9 May 2008 15:02:50 -0700 Subject: GEM: Avoid leaking refs on target objects on presumed offset success. --- linux-core/i915_gem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index ff8e2762..be30d6bf 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -640,8 +640,10 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, /* If the relocation already has the right value in it, no * more work needs to be done. */ - if (target_obj_priv->gtt_offset == reloc.presumed_offset) + if (target_obj_priv->gtt_offset == reloc.presumed_offset) { + drm_gem_object_unreference(target_obj); continue; + } /* Now that we're going to actually write some data in, * make sure that any rendering using this buffer's contents -- cgit v1.2.3 From f56f2acb5a3f34ad6916ff315d3d2058bd4b8f9c Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 9 May 2008 15:07:49 -0700 Subject: GEM: Clear obj_priv->agp_mem when we free it. Still managing to get something wrong with this, oopsing down in agp. --- linux-core/i915_gem.c | 1 + 1 file changed, 1 insertion(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index be30d6bf..caead91f 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -211,6 +211,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); + obj_priv->agp_mem = NULL; } i915_gem_object_free_page_list(obj); -- cgit v1.2.3 From c5c59eab809604e4d0d4d1dc71fc11186d0220f8 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 9 May 2008 14:34:20 -0700 Subject: GEM: Separate the LRU into execution list and LRU list. Now, the LRU list has objects that are completely done rendering and ready to kick out, while the execution list has things with active rendering, which have associated cookies and reference counts on them. --- linux-core/i915_gem.c | 130 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 97 insertions(+), 33 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index caead91f..ec5a9872 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -150,6 +150,7 @@ static int i915_gem_object_wait_rendering(struct drm_gem_object *obj) { struct drm_device *dev = obj->dev; + drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; int ret; @@ -163,9 +164,19 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) #endif i915_gem_flush(dev, 0, obj->write_domain); obj->write_domain = 0; + + /* Add a reference since we're gaining a cookie. */ if (obj_priv->last_rendering_cookie == 0) drm_gem_object_reference(obj); + /* Move from whatever list we were on to the tail of execution. + */ + list_move_tail(&obj_priv->gtt_lru_entry, + &dev_priv->mm.execution_list); obj_priv->last_rendering_cookie = i915_emit_irq(dev); + BUG_ON(obj_priv->last_rendering_cookie == 0); +#if WATCH_LRU + DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj); +#endif } /* If there is rendering queued on the buffer being evicted, wait for * it. @@ -178,12 +189,19 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) ret = i915_wait_irq(dev, obj_priv->last_rendering_cookie); if (ret != 0) return ret; + /* Clear it now that we know it's passed. */ obj_priv->last_rendering_cookie = 0; - - /* - * The cookie held a reference to the object, - * release that now + /* We were on the execution list since we had a cookie. + * Move to the tail of the LRU list now since we're done. + */ + list_move_tail(&obj_priv->gtt_lru_entry, + &dev_priv->mm.gtt_lru); +#if WATCH_LRU + DRM_INFO("%s: wait moves to lru list %p\n", __func__, obj); +#endif + /* The cookie held a reference to the object, release that + * now */ drm_gem_object_unreference(obj); } @@ -206,7 +224,11 @@ i915_gem_object_unbind(struct drm_gem_object *obj) if (obj_priv->gtt_space == NULL) return; - i915_gem_object_wait_rendering(obj); + /* Ignore the return value of wait_rendering. If we're here but + * a wait_rendering hasn't completed, we're in the freeing process, + * and we want the buffer to go away even if the command queue is hung. + */ + (void)i915_gem_object_wait_rendering(obj); if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); @@ -218,8 +240,17 @@ i915_gem_object_unbind(struct drm_gem_object *obj) drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; - if (!list_empty(&obj_priv->gtt_lru_entry)) + + /* Remove ourselves from the LRU list if present. */ + if (!list_empty(&obj_priv->gtt_lru_entry)) { list_del_init(&obj_priv->gtt_lru_entry); + if (obj_priv->last_rendering_cookie) { + DRM_ERROR("Failed to wait on buffer when unbinding, " + "continued anyway.\n"); + obj_priv->last_rendering_cookie = 0; + drm_gem_object_unreference(obj); + } + } } #if WATCH_BUF | WATCH_EXEC @@ -274,6 +305,13 @@ i915_dump_lru(struct drm_device *dev, const char *where) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv; + DRM_INFO("GTT execution list %s {\n", where); + list_for_each_entry(obj_priv, &dev_priv->mm.execution_list, + gtt_lru_entry) + { + DRM_INFO(" %p: %08x\n", obj_priv, + obj_priv->last_rendering_cookie); + } DRM_INFO("GTT LRU %s {\n", where); list_for_each_entry(obj_priv, &dev_priv->mm.gtt_lru, gtt_lru_entry) { DRM_INFO(" %p: %08x\n", obj_priv, @@ -292,11 +330,22 @@ i915_gem_evict_something(struct drm_device *dev) int ret; /* Find the LRU buffer. */ - BUG_ON(list_empty(&dev_priv->mm.gtt_lru)); - obj_priv = list_first_entry(&dev_priv->mm.gtt_lru, - struct drm_i915_gem_object, - gtt_lru_entry); + if (!list_empty(&dev_priv->mm.gtt_lru)) { + obj_priv = list_first_entry(&dev_priv->mm.gtt_lru, + struct drm_i915_gem_object, + gtt_lru_entry); + } else if (!list_empty(&dev_priv->mm.execution_list)) { + /* If there's nothing unused and ready, grab the LRU + * from the currently executing list. + */ + obj_priv = list_first_entry(&dev_priv->mm.execution_list, + struct drm_i915_gem_object, + gtt_lru_entry); + } else { + return -ENOMEM; + } obj = obj_priv->obj; + drm_gem_object_reference(obj); #if WATCH_LRU DRM_INFO("%s: evicting %p\n", __func__, obj); #endif @@ -317,6 +366,7 @@ i915_gem_evict_something(struct drm_device *dev) #if WATCH_LRU DRM_INFO("%s: evicted %p\n", __func__, obj); #endif + drm_gem_object_unreference(obj); return 0; } @@ -360,7 +410,8 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) #if WATCH_LRU DRM_INFO("%s: GTT full, evicting something\n", __func__); #endif - if (list_empty(&dev_priv->mm.gtt_lru)) { + if (list_empty(&dev_priv->mm.gtt_lru) && + list_empty(&dev_priv->mm.execution_list)) { DRM_ERROR("GTT full, but LRU list empty\n"); return -ENOMEM; } @@ -529,7 +580,6 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, struct drm_i915_gem_validate_entry *entry) { struct drm_device *dev = obj->dev; - drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_relocation_entry reloc; struct drm_i915_gem_relocation_entry __user *relocs; struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -546,19 +596,6 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, entry->buffer_offset = obj_priv->gtt_offset; - if (obj_priv->pin_count == 0) { - /* Move our buffer to the head of the LRU. */ - if (list_empty(&obj_priv->gtt_lru_entry)) - list_add_tail(&obj_priv->gtt_lru_entry, - &dev_priv->mm.gtt_lru); - else - list_move_tail(&obj_priv->gtt_lru_entry, - &dev_priv->mm.gtt_lru); -#if WATCH_LRU && 0 - i915_dump_lru(dev, __func__); -#endif - } - relocs = (struct drm_i915_gem_relocation_entry __user *) (uintptr_t) entry->relocs_ptr; /* Apply the relocations, using the GTT aperture to avoid cache @@ -770,6 +807,7 @@ int i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) { + drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_execbuffer *args = data; struct drm_i915_gem_validate_entry *validate_list; struct drm_gem_object **object_list; @@ -894,7 +932,17 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, if (obj_priv->last_rendering_cookie == 0) drm_gem_object_reference(obj); obj_priv->last_rendering_cookie = cookie; + BUG_ON(obj_priv->last_rendering_cookie == 0); + /* Move our buffer to the tail of the execution list. */ + list_move_tail(&obj_priv->gtt_lru_entry, + &dev_priv->mm.execution_list); +#if WATCH_LRU + DRM_INFO("%s: move to exec list %p\n", __func__, obj); +#endif } +#if WATCH_LRU && 0 + i915_dump_lru(dev, __func__); +#endif /* Copy the new buffer offsets back to the user's validate list. */ ret = copy_to_user((struct drm_i915_relocation_entry __user *) @@ -911,14 +959,6 @@ err: drm_gem_object_unreference(object_list[i]); } -#if 1 - /* XXX kludge for now as we don't clean the exec ring yet */ - if (object_list != NULL) { - for (i = 0; i < args->buffer_count; i++) - i915_gem_object_wait_rendering(object_list[i]); - } -#endif - drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, @@ -1014,3 +1054,27 @@ i915_gem_set_domain_ioctl(struct drm_gem_object *obj, i915_gem_dev_set_domain(obj->dev); return 0; } + +void +i915_gem_lastclose(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + + /* Assume that the chip has been idled at this point. Just pull them + * off the execution list and unref them. Since this is the last + * close, this is also the last ref and they'll go away. + */ + + while (!list_empty(&dev_priv->mm.execution_list)) { + struct drm_i915_gem_object *obj_priv; + + obj_priv = list_first_entry(&dev_priv->mm.execution_list, + struct drm_i915_gem_object, + gtt_lru_entry); + + list_del_init(&obj_priv->gtt_lru_entry); + obj_priv->last_rendering_cookie = 0; + obj_priv->obj->write_domain = 0; + drm_gem_object_unreference(obj_priv->obj); + } +} -- cgit v1.2.3 From 48a8531aa403ea250696338aa8717e3e36477370 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 9 May 2008 18:23:51 -0700 Subject: GEM: Fix arguments to drm_memrange_init so we don't exceed our allocation. It takes (offset, size), not (offset, end). --- linux-core/i915_gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index ec5a9872..37a4e503 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -48,7 +48,7 @@ i915_gem_init_ioctl(struct drm_device *dev, void *data, return -EINVAL; drm_memrange_init(&dev_priv->mm.gtt_space, args->gtt_start, - args->gtt_end); + args->gtt_end - args->gtt_start); return 0; } -- cgit v1.2.3 From a37ac493da1730436028ecc79a38513380ce15d0 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 10 May 2008 21:04:18 -0700 Subject: [intel-GEM] Clean up GEM ioctl naming. Rename 'validate_entry' to 'exec_object', then clean up some field names in structures (renaming buffer_offset to just offset, for example). --- linux-core/i915_gem.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index ff8e2762..e91cae30 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -525,7 +525,7 @@ i915_gem_dev_set_domain(struct drm_device *dev) static int i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, struct drm_file *file_priv, - struct drm_i915_gem_validate_entry *entry) + struct drm_i915_gem_exec_object *entry) { struct drm_device *dev = obj->dev; drm_i915_private_t *dev_priv = dev->dev_private; @@ -543,7 +543,7 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, return -ENOMEM; } - entry->buffer_offset = obj_priv->gtt_offset; + entry->offset = obj_priv->gtt_offset; if (obj_priv->pin_count == 0) { /* Move our buffer to the head of the LRU. */ @@ -768,7 +768,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) { struct drm_i915_gem_execbuffer *args = data; - struct drm_i915_gem_validate_entry *validate_list; + struct drm_i915_gem_exec_object *validate_list; struct drm_gem_object **object_list; struct drm_gem_object *batch_obj; int ret, i; @@ -808,11 +808,10 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* Look up object handles and perform the relocations */ for (i = 0; i < args->buffer_count; i++) { object_list[i] = drm_gem_object_lookup(dev, file_priv, - validate_list[i]. - buffer_handle); + validate_list[i].handle); if (object_list[i] == NULL) { DRM_ERROR("Invalid object handle %d at index %d\n", - validate_list[i].buffer_handle, i); + validate_list[i].handle, i); ret = -EINVAL; goto err; } @@ -856,7 +855,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* Flush/invalidate caches and chipset buffer */ i915_gem_dev_set_domain(dev); - exec_offset = validate_list[args->buffer_count - 1].buffer_offset; + exec_offset = validate_list[args->buffer_count - 1].offset; #if WATCH_EXEC i915_gem_dump_object(object_list[args->buffer_count - 1], -- cgit v1.2.3 From 177b8b07033c56c84d335808121690d235516bb5 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 10 May 2008 21:04:42 -0700 Subject: [GEM] Add drm-gem.txt Add some API and implementation documentation for GEM. --- linux-core/drm-gem.txt | 799 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 799 insertions(+) create mode 100644 linux-core/drm-gem.txt (limited to 'linux-core') diff --git a/linux-core/drm-gem.txt b/linux-core/drm-gem.txt new file mode 100644 index 00000000..8f95c206 --- /dev/null +++ b/linux-core/drm-gem.txt @@ -0,0 +1,799 @@ + The Graphics Execution Manager + Part of the Direct Rendering Manager + ============================== + + Keith Packard + Eric Anholt + 2008-5-9 + +Contents: + + 1. GEM Overview + 2. API overview and conventions + 3. Object Creation/Destruction + 4. Reading/writing contents + 5. Mapping objects to userspace + 6. Memory Domains + 7. Execution (Intel specific) + 8. Other misc Intel-specific functions + +1. Graphics Execution Manager Overview + +Gem is designed to manage graphics memory, control access to the graphics +device execution context and handle the essentially NUMA environment unique +to modern graphics hardware. Gem allows multiple applications to share +graphics device resources without the need to constantly reload the entire +graphics card. Data may be shared between multiple applications with gem +ensuring that the correct memory synchronization occurs. + +Graphics data can consume arbitrary amounts of memory, with 3D applications +constructing ever larger sets of textures and vertices. With graphics cards +memory space growing larger every year, and graphics APIs growing more +complex, we can no longer insist that each application save a complete copy +of their graphics state so that the card can be re-initialized from user +space at each context switch. Ensuring that graphics data remains persistent +across context switches allows applications significant new functionality +while also improving performance for existing APIs. + +Modern linux desktops include significant 3D rendering as a fundemental +component of the desktop image construction process. 2D and 3D applications +paint their content to offscreen storage and the central 'compositing +manager' constructs the final screen image from those window contents. This +means that pixel image data from these applications must move within reach +of the compositing manager and used as source operands for screen image +rendering operations. + +Gem provides simple mechanisms to manage graphics data and control execution +flow within the linux operating system. Using many existing kernel +subsystems, it does this with a modest amount of code. + +2. API Overview and Conventions + +All APIs here are defined in terms of ioctls appplied to the DRM file +descriptor. To create and manipulate objects, an applications must be +'authorized' using the DRI or DRI2 protocols with the X server. To relax +that, we will need to implement some better access control mechanisms within +the hardware portion of the driver to prevent inappropriate +cross-application data access. + +Any DRM driver which does not support GEM will return -ENODEV for all of +these ioctls. Invalid object handles return -EINVAL. Invalid object names +return -ENOENT. Other errors are as documented in the specific API below. + +To avoid the need to translate ioctl contents on mixed-size systems (with +32-bit user space running on a 64-bit kernel), the ioctl data structures +contain explicitly sized objects, using 64-bits for all size and pointer +data and 32-bits for identifiers. In addition, the 64-bit objects are all +carefully aligned on 64-bit boundaries. Because of this, all pointers in the +ioctl data structures are passed as uint64_t values. Suitable casts will +be necessary. + +One significant operation which is explicitly left out of this API is object +locking. Applications are expected to perform locking of shared objects +outside of the GEM api. This kind of locking is not necessary to safely +manipulate the graphics engine, and with multiple objects interacting in +unknown ways, per-object locking would likely introduce all kinds of +lock-order issues. Punting this to the application seems like the only +sensible plan. Given that DRM already offers a global lock on the hardware, +this doesn't change the current situation. + +3. Object Creation and Destruction + +Gem provides explicit memory management primitives. System pages are +allocated when the object is created, either as the fundemental storage for +hardware where system memory is used by the graphics processor directly, or +as backing store for graphics-processor resident memory. + +Objects are referenced from user space using handles. These are, for all +intents and purposes, equivalent to file descriptors. We could simply use +file descriptors were it not for the small limit (1024) of file descriptors +available to applications, and for the fact that the X server (a rather +significant user of this API) uses 'select' and has a limited maximum file +descriptor for that operation. Given the ability to allocate more file +descriptors, and given the ability to place these 'higher' in the file +descriptor space, we'd love to simply use file descriptors. + +Objects may be published with a name so that other applications can access +them. The name remains valid as long as the object exists. Right now, our +DRI APIs use 32-bit integer names, so that's what we expose here + + A. Creation + + struct drm_gem_create { + /** + * Requested size for the object. + * + * The (page-aligned) allocated size for the object + * will be returned. + */ + uint64_t size; + /** + * Returned handle for the object. + * + * Object handles are nonzero. + */ + uint32_t handle; + uint32_t pad; + }; + + /* usage */ + create.size = 16384; + ret = ioctl (fd, DRM_IOCTL_GEM_CREATE, &create); + if (ret == 0) + return create.handle; + + Note that the size is rounded up to a page boundary, and that + the rounded-up size is returned in 'size'. No name is assigned to + this object, making it local to this process. + + If insufficient memory is availabe, -ENOMEM will be returned. + + B. Closing + + struct drm_gem_close { + /** Handle of the object to be closed. */ + uint32_t handle; + uint32_t pad; + }; + + + /* usage */ + close.handle = ; + ret = ioctl (fd, DRM_IOCTL_GEM_CLOSE, &close); + + This call makes the specified handle invalid, and if no other + applications are using the object, any necessary graphics hardware + synchronization is performed and the resources used by the object + released. + + C. Naming + + struct drm_gem_flink { + /** Handle for the object being named */ + uint32_t handle; + + /** Returned global name */ + uint32_t name; + }; + + /* usage */ + flink.handle = ; + ret = ioctl (fd, DRM_IOCTL_GEM_FLINK, &flink); + if (ret == 0) + return flink.name; + + Flink creates a name for the object and returns it to the + application. This name can be used by other applications to gain + access to the same object. + + D. Opening by name + + struct drm_gem_open { + /** Name of object being opened */ + uint32_t name; + + /** Returned handle for the object */ + uint32_t handle; + + /** Returned size of the object */ + uint64_t size; + }; + + /* usage */ + open.name = ; + ret = ioctl (fd, DRM_IOCTL_GEM_OPEN, &open); + if (ret == 0) { + *sizep = open.size; + return open.handle; + } + + Open accesses an existing object and returns a handle for it. If the + object doesn't exist, -ENOENT is returned. The size of the object is + also returned. This handle has all the same capabilities as the + handle used to create the object. In particular, the object is not + destroyed until all handles are closed. + +4. Basic read/write operations + +By default, gem objects are not mapped to the applications address space, +getting data in and out of them is done with I/O operations instead. This +allows the data to reside in otherwise unmapped pages, including pages in +video memory on an attached discrete graphics card. In addition, using +explicit I/O operations allows better control over cache contents, as +graphics devices are generally not cache coherent with the CPU, mapping +pages used for graphics into an application address space requires the use +of expensive cache flushing operations. Providing direct control over +graphics data access ensures that data are handled in the most efficient +possible fashion. + + A. Reading + + struct drm_gem_pread { + /** Handle for the object being read. */ + uint32_t handle; + uint32_t pad; + /** Offset into the object to read from */ + uint64_t offset; + /** Length of data to read */ + uint64_t size; + /** Pointer to write the data into. */ + uint64_t data_ptr; /* void * */ + }; + + This copies data into the specified object at the specified + position. Any necessary graphics device synchronization and + flushing will be done automatically. + + struct drm_gem_pwrite { + /** Handle for the object being written to. */ + uint32_t handle; + uint32_t pad; + /** Offset into the object to write to */ + uint64_t offset; + /** Length of data to write */ + uint64_t size; + /** Pointer to read the data from. */ + uint64_t data_ptr; /* void * */ + }; + + This copies data out of the specified object into the + waiting user memory. Again, device synchronization will + be handled by the kernel to ensure user space sees a + consistent view of the graphics device. + +5. Mapping objects to user space + +For most objects, reading/writing is the preferred interaction mode. +However, when the CPU is involved in rendering to cover deficiencies in +hardware support for particular operations, the CPU will want to directly +access the relevant objects. + +Because mmap is fairly heavyweight, we allow applications to retain maps to +objects persistently and then update how they're using the memory through a +separate interface. Applications which fail to use this separate interface +may exhibit unpredictable behaviour as memory consistency will not be +preserved. + + A. Mapping + + struct drm_gem_mmap { + /** Handle for the object being mapped. */ + uint32_t handle; + uint32_t pad; + /** Offset in the object to map. */ + uint64_t offset; + /** + * Length of data to map. + * + * The value will be page-aligned. + */ + uint64_t size; + /** Returned pointer the data was mapped at */ + uint64_t addr_ptr; /* void * */ + }; + + /* usage */ + mmap.handle = ; + mmap.offset = ; + mmap.size = ; + ret = ioctl (fd, DRM_IOCTL_GEM_MMAP, &mmap); + if (ret == 0) + return (void *) (uintptr_t) mmap.addr_ptr; + + + B. Unmapping + + munmap (addr, length); + + Nothing strange here, just use the normal munmap syscall. + +6. Memory Domains + +Graphics devices remain a strong bastion of non cache-coherent memory. As a +result, accessing data through one functional unit will end up loading that +cache with data which then needs to be manually synchronized when that data +is used with another functional unit. + +Tracking where data are resident is done by identifying how functional units +deal with caches. Each cache is labeled as a separate memory domain. Then, +each sequence of operations is expected to load data into various read +domains and leave data in at most one write domain. Gem tracks the read and +write memory domains of each object and performs the necessary +synchronization operations when objects move from one domain set to another. + +For example, if operation 'A' constructs an image that is immediately used +by operation 'B', then when the read domain for 'B' is not the same as the +write domain for 'A', then the write domain must be flushed, and the read +domain invalidated. If these two operations are both executed in the same +command queue, then the flush operation can go inbetween them in the same +queue, avoiding any kind of CPU-based synchronization and leaving the GPU to +do the work itself. + +6.1 Memory Domains (GPU-independent) + + * DRM_GEM_DOMAIN_CPU. + + Objects in this domain are using caches which are connected to the CPU. + Moving objects from non-CPU domains into the CPU domain can involve waiting + for the GPU to finish with operations using this object. Moving objects + from this domain to a GPU domain can involve flushing CPU caches and chipset + buffers. + +6.1 GPU-independent memory domain ioctl + +This ioctl is independent of the GPU in use. So far, no use other than +synchronizing objects to the CPU domain have been found; if that turns out +to be generally true, this ioctl may be simplified further. + + A. Explicit domain control + + struct drm_gem_set_domain { + /** Handle for the object */ + uint32_t handle; + + /** New read domains */ + uint32_t read_domains; + + /** New write domain */ + uint32_t write_domain; + }; + + /* usage */ + set_domain.handle = ; + set_domain.read_domains = ; + set_domain.write_domain = ; + ret = ioctl (fd, DRM_IOCTL_GEM_SET_DOMAIN, &set_domain); + + When the application wants to explicitly manage memory domains for + an object, it can use this function. Usually, this is only used + when the application wants to synchronize object contents between + the GPU and CPU-based application rendering. In that case, + the would be set to DRM_GEM_DOMAIN_CPU, and if the + application were going to write to the object, the + would also be set to DRM_GEM_DOMAIN_CPU. After the call, gem + guarantees that all previous rendering operations involving this + object are complete. The application is then free to access the + object through the address returned by the mmap call. Afterwards, + when the application again uses the object through the GPU, any + necessary CPU flushing will occur and the object will be correctly + synchronized with the GPU. + +7. Execution (Intel specific) + +Managing the command buffers is inherently chip-specific, so the core of gem +doesn't have any intrinsic functions. Rather, execution is left to the +device-specific portions of the driver. + +The Intel DRM_I915_GEM_EXECBUFFER ioctl takes a list of gem objects, all of +which are mapped to the graphics device. The last object in the list is the +command buffer. + +7.1. Relocations + +Command buffers often refer to other objects, and to allow the kernel driver +to move objects around, a sequence of relocations is associated with each +object. Device-specific relocation operations are used to place the +target-object relative value into the object. + +The Intel driver has a single relocation type: + + struct drm_i915_gem_relocation_entry { + /* + * Handle of the buffer being pointed to by this + * relocation entry. + /* + * It's appealing to make this be an index into the + * mm_validate_entry list to refer to the buffer, but + * handle lookup should be O(1) anyway, and prevents + * O(n) search in userland to find what that index is. + + */ + uint32_t target_handle; + + /** + * Value to be added to the offset of the target + * buffer to make up the relocation entry. + */ + uint32_t delta; + + /** + * Offset in the buffer the relocation entry will be + * written into + */ + uint64_t offset; + + /** + * Offset value of the target buffer that the + * relocation entry was last written as. + * + * If the buffer has the same offset as last time, we + * can skip syncing and writing the relocation. This + * value is written back out by the execbuffer ioctl + * when the relocation is written. + */ + uint64_t presumed_offset; + + /** + * Target memory domains read by this operation. + */ + uint32_t read_domains; + + /* + * Target memory domains written by this operation. + * + * Note that only one domain may be written by the + * whole execbuffer operation, so that where there are + * conflicts, the application will get -EINVAL back. + */ + uint32_t write_domain; + }; + + 'target_handle', the handle to the target object. This object must + be one of the objects listed in the execbuffer request or + bad things will happen. The kernel doesn't check for this. + + 'offset' is where, in the source object, the relocation data + are written. Each relocation value is a 32-bit value consisting + of the location of the target object in the GPU memory space plus + the 'delta' value included in the relocation. + + 'presumed_offset' is where user-space believes the target object + lies in GPU memory space. If this value matches where the object + actually is, then no relocation data are written, the kernel + assumes that user space has set up data in the source object + using this presumption. This offers a fairly important optimization + as writing relocation data requires mapping of the source object + into the kernel memory space. + + 'read_domains' and 'write_domains' list the usage by the source + object of the target object. The kernel unions all of the domain + information from all relocations in the execbuffer request. No more + than one write_domain is allowed, otherwise an EINVAL error is + returned. read_domains must contain write_domain. This domain + information is used to synchronize buffer contents as described + above in the section on domains. + +7.1.1 Memory Domains (Intel specific) + +The Intel GPU has several internal caches which are not coherent and hence +require explicit synchronization. Memory domains provide the necessary data +to synchronize what is needed while leaving other cache contents intact. + + * DRM_GEM_DOMAIN_I915_RENDER. + The GPU 3D and 2D rendering operations use a unified rendering cache, so + operations doing 3D painting and 2D blts will use this domain + + * DRM_GEM_DOMAIN_I915_SAMPLER + Textures are loaded by the sampler through a separate cache, so + any texture reading will use this domain. Note that the sampler + and renderer use different caches, so moving an object from render target + to texture source will require a domain transfer. + + * DRM_GEM_DOMAIN_I915_COMMAND + The command buffer doesn't have an explicit cache (although it does + read ahead quite a bit), so this domain just indicates that the object + needs to be flushed to the GPU. + + * DRM_GEM_DOMAIN_I915_INSTRUCTION + Fragment programs on Gen3 and all of the programs on later + chips use an instruction cache to speed program execution. It must be + explicitly flushed when new programs are written to memory by the CPU. + + * DRM_GEM_DOMAIN_I915_VERTEX + Vertex data uses two different vertex caches, but they're + both flushed with the same instruction. + +7.2 Execution object list (Intel specific) + + struct drm_i915_gem_exec_object { + /** + * User's handle for a buffer to be bound into the GTT + * for this operation. + */ + uint32_t handle; + + /** + * List of relocations to be performed on this buffer + */ + uint32_t relocation_count; + /* struct drm_i915_gem_relocation_entry *relocs */ + uint64_t relocs_ptr; + + /** + * Required alignment in graphics aperture + */ + uint64_t alignment; + + /** + * Returned value of the updated offset of the object, + * for future presumed_offset writes. + */ + uint64_t offset; + }; + + Each object involved in a particular execution operation must be + listed using one of these structures. + + 'handle' references the object. + + 'relocs_ptr' is a user-mode pointer to a array of 'relocation_count' + drm_i915_gem_relocation_entry structs (see above) that + define the relocations necessary in this buffer. Note that all + relocations must reference other exec_object structures in the same + execbuffer ioctl and that those other buffers must come earlier in + the exec_object array. In other words, the dependencies mapped by the + exec_object relocations must form a directed acyclic graph. + + 'alignment' is the byte alignment necessary for this buffer. Each + object has specific alignment requirements, as the kernel doesn't + know what each object is being used for, those requirements must be + provided by user mode. If an object is used in two different ways, + it's quite possible that the alignment requirements will differ. + + 'offset' is a return value, receiving the location of the object + during this execbuffer operation. The application should use this + as the presumed offset in future operations; if the object does not + move, then kernel need not write relocation data. + +7.3 Execbuffer ioctl (Intel specific) + + struct drm_i915_gem_execbuffer { + /** + * List of buffers to be validated wit their + * relocations to be performend on them. + * + * These buffers must be listed in an order such that + * all relocations a buffer is performing refer to + * buffers that have already appeared in the validate + * list. + */ + /* struct drm_i915_gem_validate_entry *buffers */ + uint64_t buffers_ptr; + uint32_t buffer_count; + + /** + * Offset in the batchbuffer to start execution from. + */ + uint32_t batch_start_offset; + + /** + * Bytes used in batchbuffer from batch_start_offset + */ + uint32_t batch_len; + uint32_t DR1; + uint32_t DR4; + uint32_t num_cliprects; + uint64_t cliprects_ptr; /* struct drm_clip_rect *cliprects */ + }; + + + 'buffers_ptr' is a user-mode pointer to an array of 'buffer_count' + drm_i915_gem_exec_object structures which contains the complete set + of objects required for this execbuffer operation. The last entry in + this array, the 'batch buffer', is the buffer of commands which will + be linked to the ring and executed. + + 'batch_start_offset' is the byte offset within the batch buffer which + contains the first command to execute. So far, we haven't found a + reason to use anything other than '0' here, but the thought was that + some space might be allocated for additional initialization which + could be skipped in some cases. This must be a multiple of 4. + + 'batch_len' is the length, in bytes, of the data to be executed + (i.e., the amount of data after batch_start_offset). This must + be a multiple of 4. + + 'num_cliprects' and 'cliprects_ptr' reference an array of + drm_clip_rect structures that is num_cliprects long. The entire + batch buffer will be executed multiple times, once for each + rectangle in this list. If num_cliprects is 0, then no clipping + rectangle will be set. + + 'DR1' and 'DR4' are portions of the 3DSTATE_DRAWING_RECTANGLE + command which will be queued when this operation is clipped + (num_cliprects != 0). + + DR1 bit definition + 31 Fast Scissor Clip Disable (debug only). + Disables a hardware optimization that + improves performance. This should have + no visible effect, other than reducing + performance + + 30 Depth Buffer Coordinate Offset Disable. + This disables the addition of the + depth buffer offset bits which are used + to change the location of the depth buffer + relative to the front buffer. + + 27:26 X Dither Offset. Specifies the X pixel + offset to use when accessing the dither table + + 25:24 Y Dither Offset. Specifies the Y pixel + offset to use when accessing the dither + table. + + DR4 bit definition + 31:16 Drawing Rectangle Origin Y. Specifies the Y + origin of coordinates relative to the + draw buffer. + + 15:0 Drawing Rectangle Origin X. Specifies the X + origin of coordinates relative to the + draw buffer. + + As you can see, these two fields are necessary for correctly + offsetting drawing within a buffer which contains multiple surfaces. + Note that DR1 is only used on Gen3 and earlier hardware and that + newer hardware sticks the dither offset elsewhere. + +7.3.1 Detailed Execution Description + + Execution of a single batch buffer requires several preparatory + steps to make the objects visible to the graphics engine and resolve + relocations to account for their current addresses. + + A. Mapping and Relocation + + Each exec_object structure in the array is examined in turn. + + If the object is not already bound to the GTT, it is assigned a + location in the graphics address space. If no space is available in + the GTT, some other object will be evicted. This may require waiting + for previous execbuffer requests to complete before that object can + be unmapped. With the location assigned, the pages for the object + are pinned in memory using find_or_create_page and the GTT entries + updated to point at the relevant pages using drm_agp_bind_pages. + + Then the array of relocations is traversed. Each relocation record + looks up the target object and, if the presumed offset does not + match the current offset (remember that this buffer has already been + assigned an address as it must have been mapped earlier), the + relocation value is computed using the current offset. If the + object is currently in use by the graphics engine, writing the data + out must be preceeded by a delay while the object is still busy. + Once it is idle, then the page containing the relocation is mapped + by the CPU and the updated relocation data written out. + + The read_domains and write_domain entries in each relocation are + used to compute the new read_domains and write_domain values for the + target buffers. The actual execution of the domain changes must wait + until all of the exec_object entries have been evaluated as the + complete set of domain information will not be available until then. + + B. Memory Domain Resolution + + After all of the new memory domain data has been pulled out of the + relocations and computed for each object, the list of objects is + again traversed and the new memory domains compared against the + current memory domains. There are two basic operations involved here: + + * Flushing the current write domain. If the new read domains + are not equal to the current write domain, then the current + write domain must be flushed. Otherwise, reads will not see data + present in the write domain cache. In addition, any new read domains + other than the current write domain must be invalidated to ensure + that the flushed data are re-read into their caches. + + * Invaliding new read domains. Any domains which were not currently + used for this object must be invalidated as old objects which + were mapped at the same location may have stale data in the new + domain caches. + + If the CPU cache is being invalidated and some GPU cache is being + flushed, then we'll have to wait for rendering to complete so that + any pending GPU writes will be complete before we flush the GPU + cache. + + If the CPU cache is being flushed, then we use 'clflush' to get data + written from the CPU. + + Because the GPU caches cannot be partially flushed or invalidated, + we don't actually flush them during this traversal stage. Rather, we + gather the invalidate and flush bits up in the device structure. + + Once all of the object domain changes have been evaluated, then the + gathered invalidate and flush bits are examined. For any GPU flush + operations, we emit a single MI_FLUSH command that performs all of + the necessary flushes. We then look to see if the CPU cache was + flushed. If so, we use the chipset flush magic (writing to a special + page) to get the data out of the chipset and into memory. + + C. Queuing Batch Buffer to the Ring + + With all of the objects resident in graphics memory space, and all + of the caches prepared with appropriate data, the batch buffer + object can be queued to the ring. If there are clip rectangles, then + the buffer is queued once per rectangle, with suitable clipping + inserted into the ring just before the batch buffer. + + D. Creating an IRQ Cookie + + Right after the batch buffer is placed in the ring, a request to + generate an IRQ is added to the ring along with a command to write a + marker into memory. When the IRQ fires, the driver can look at the + memory location to see where in the ring the GPU has passed. This + magic cookie value is stored in each object used in this execbuffer + command; it is used whereever you saw 'wait for rendering' above in + this document. + + E. Writing back the new object offsets + + So that the application has a better idea what to use for + 'presumed_offset' values later, the current object offsets are + written back to the exec_object structures. + + +8. Other misc Intel-specific functions. + +To complete the driver, a few other functions were necessary. + +8.1 Initialization from the X server + +As the X server is currently responsible for apportioning memory between 2D +and 3D, it must tell the kernel which region of the GTT aperture is +available for 3D objects to be mapped into. + + struct drm_i915_gem_init { + /** + * Beginning offset in the GTT to be managed by the + * DRM memory manager. + */ + uint64_t gtt_start; + /** + * Ending offset in the GTT to be managed by the DRM + * memory manager. + */ + uint64_t gtt_end; + }; + /* usage */ + init.gtt_start = ; + init.gtt_end = ; + ret = ioctl (fd, DRM_IOCTL_I915_GEM_INIT, &init); + + The GTT aperture between gtt_start and gtt_end will be used to map + objects. This also tells the kernel that the ring can be used, + pulling the ring addresses from the device registers. + +8.2 Pinning objects in the GTT + +For scan-out buffers and the current shared depth and back buffers, we need +to have them always available in the GTT, at least for now. Pinning means to +lock their pages in memory along with keeping them at a fixed offset in the +graphics aperture. These operations are available only to root. + + struct drm_i915_gem_pin { + /** Handle of the buffer to be pinned. */ + uint32_t handle; + uint32_t pad; + + /** alignment required within the aperture */ + uint64_t alignment; + + /** Returned GTT offset of the buffer. */ + uint64_t offset; + }; + + /* usage */ + pin.handle = ; + pin.alignment = ; + ret = ioctl (fd, DRM_IOCTL_I915_GEM_PIN, &pin); + if (ret == 0) + return pin.offset; + + Pinning an object ensures that it will not be evicted from the GTT + or moved. It will stay resident until destroyed or unpinned. + + struct drm_i915_gem_unpin { + /** Handle of the buffer to be unpinned. */ + uint32_t handle; + uint32_t pad; + }; + + /* usage */ + unpin.handle = ; + ret = ioctl (fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin); + + Unpinning an object makes it possible to evict this object from the + GTT. It doesn't ensure that it will be evicted, just that it may. + -- cgit v1.2.3 From 1b0bf301431e76712de1ee43681bc818383b2e56 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 10 May 2008 22:04:39 -0700 Subject: [intel-GEM] exec list can contain pinned, lru cannot. The exec list contains all objects, in order of use. The lru list contains only unpinned objects ready to be evicted. This required two changes -- the first was to not migrate pinned objects from exec to lru, the second was to search for the first unpinned object in the exec list when doing eviction. --- linux-core/i915_gem.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 0cacc65b..90d1d52b 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -192,11 +192,14 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) /* Clear it now that we know it's passed. */ obj_priv->last_rendering_cookie = 0; + /* We were on the execution list since we had a cookie. * Move to the tail of the LRU list now since we're done. */ - list_move_tail(&obj_priv->gtt_lru_entry, - &dev_priv->mm.gtt_lru); + if (obj_priv->pin_count == 0) + list_move_tail(&obj_priv->gtt_lru_entry, + &dev_priv->mm.gtt_lru); + #if WATCH_LRU DRM_INFO("%s: wait moves to lru list %p\n", __func__, obj); #endif @@ -335,12 +338,15 @@ i915_gem_evict_something(struct drm_device *dev) struct drm_i915_gem_object, gtt_lru_entry); } else if (!list_empty(&dev_priv->mm.execution_list)) { - /* If there's nothing unused and ready, grab the LRU - * from the currently executing list. + /* If there's nothing unused and ready, grab the first + * unpinned object from the currently executing list. */ - obj_priv = list_first_entry(&dev_priv->mm.execution_list, - struct drm_i915_gem_object, - gtt_lru_entry); + list_for_each_entry(obj_priv, &dev_priv->mm.execution_list, + gtt_lru_entry) + if (obj_priv->pin_count == 0) + break; + if (!obj_priv) + return -ENOMEM; } else { return -ENOMEM; } -- cgit v1.2.3 From ff39db099b9ca6c8feee68101a2269345b7bd798 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 11 May 2008 00:10:16 -0700 Subject: [GEM] Make pread/pwrite manage memory domains. No luck with movnti though. pread and pwrite must update the memory domains to ensure consistency with the GPU. At some point, it should be possible to avoid clflush through this path, but that isn't working for me. --- linux-core/drmP.h | 7 ++++ linux-core/drm_gem.c | 26 +++++++++++++++ linux-core/i915_drv.c | 1 + linux-core/i915_gem.c | 91 ++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 98 insertions(+), 27 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 419b4be3..fc7043d7 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -781,6 +781,13 @@ struct drm_driver { uint32_t read_domains, uint32_t write_domain); + /** + * Driver-specific callback to flush pwrite through chipset + */ + int (*gem_flush_pwrite) (struct drm_gem_object *obj, + uint64_t offset, + uint64_t size); + struct drm_fence_driver *fence_driver; struct drm_bo_driver *bo_driver; diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 6c462921..14cf7e47 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -266,6 +266,7 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, struct drm_gem_object *obj; ssize_t read; loff_t offset; + int ret; if (!(dev->driver->driver_features & DRIVER_GEM)) return -ENODEV; @@ -274,6 +275,15 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, if (obj == NULL) return -EINVAL; + if (dev->driver->gem_set_domain) { + ret = dev->driver->gem_set_domain (obj, + DRM_GEM_DOMAIN_CPU, + 0); + if (ret) { + drm_gem_object_unreference(obj); + return ret; + } + } offset = args->offset; read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr, @@ -343,6 +353,7 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, struct drm_gem_object *obj; ssize_t written; loff_t offset; + int ret; if (!(dev->driver->driver_features & DRIVER_GEM)) return -ENODEV; @@ -351,11 +362,21 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, if (obj == NULL) return -EINVAL; + if (dev->driver->gem_set_domain) { + ret = dev->driver->gem_set_domain (obj, + DRM_GEM_DOMAIN_CPU, + 0); + if (ret) { + drm_gem_object_unreference(obj); + return ret; + } + } offset = args->offset; written = vfs_write(obj->filp, (char __user *)(uintptr_t) args->data_ptr, args->size, &offset); + if (written != args->size) { drm_gem_object_unreference(obj); if (written < 0) @@ -364,6 +385,11 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, return -EINVAL; } + if (dev->driver->gem_flush_pwrite) + dev->driver->gem_flush_pwrite(obj, + args->offset, + args->size); + drm_gem_object_unreference(obj); return 0; diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index ae8cf3e0..cc47ed64 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -589,6 +589,7 @@ static struct drm_driver driver = { .gem_init_object = i915_gem_init_object, .gem_free_object = i915_gem_free_object, .gem_set_domain = i915_gem_set_domain_ioctl, + .gem_flush_pwrite = i915_gem_flush_pwrite, .fops = { .owner = THIS_MODULE, .open = drm_open, diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 90d1d52b..9d701ba1 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -377,6 +377,37 @@ i915_gem_evict_something(struct drm_device *dev) return 0; } +static int +i915_gem_object_get_page_list(struct drm_gem_object *obj) +{ + struct drm_i915_gem_object *obj_priv = obj->driver_private; + int page_count, i; + if (obj_priv->page_list) + return 0; + + /* Get the list of pages out of our struct file. They'll be pinned + * at this point until we release them. + */ + page_count = obj->size / PAGE_SIZE; + BUG_ON(obj_priv->page_list != NULL); + obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *), + DRM_MEM_DRIVER); + if (obj_priv->page_list == NULL) + return -ENOMEM; + + for (i = 0; i < page_count; i++) { + obj_priv->page_list[i] = + find_or_create_page(obj->filp->f_mapping, i, GFP_HIGHUSER); + + if (obj_priv->page_list[i] == NULL) { + i915_gem_object_free_page_list(obj); + return -ENOMEM; + } + unlock_page(obj_priv->page_list[i]); + } + return 0; +} + /** * Finds free space in the GTT aperture and binds the object there. */ @@ -387,7 +418,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; struct drm_memrange_node *free_space; - int page_count, i, ret; + int page_count, ret; if (alignment == 0) alignment = PAGE_SIZE; @@ -432,33 +463,14 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) DRM_INFO("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); #endif - - /* Get the list of pages out of our struct file. They'll be pinned - * at this point until we release them. - */ - page_count = obj->size / PAGE_SIZE; - BUG_ON(obj_priv->page_list != NULL); - obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *), - DRM_MEM_DRIVER); - if (obj_priv->page_list == NULL) { - drm_memrange_put_block(obj_priv->gtt_space); + ret = i915_gem_object_get_page_list (obj); + if (ret) { + drm_memrange_put_block (obj_priv->gtt_space); obj_priv->gtt_space = NULL; - return -ENOMEM; - } - - for (i = 0; i < page_count; i++) { - obj_priv->page_list[i] = - find_or_create_page(obj->filp->f_mapping, i, GFP_HIGHUSER); - - if (obj_priv->page_list[i] == NULL) { - i915_gem_object_free_page_list(obj); - drm_memrange_put_block(obj_priv->gtt_space); - obj_priv->gtt_space = NULL; - return -ENOMEM; - } - unlock_page(obj_priv->page_list[i]); + return ret; } + page_count = obj->size / PAGE_SIZE; /* Create an AGP memory structure pointing at our pages, and bind it * into the GTT. */ @@ -1052,14 +1064,39 @@ void i915_gem_free_object(struct drm_gem_object *obj) int i915_gem_set_domain_ioctl(struct drm_gem_object *obj, - uint32_t read_domains, - uint32_t write_domain) + uint32_t read_domains, + uint32_t write_domain) { i915_gem_object_set_domain(obj, read_domains, write_domain); i915_gem_dev_set_domain(obj->dev); return 0; } +int +i915_gem_flush_pwrite(struct drm_gem_object *obj, + uint64_t offset, uint64_t size) +{ + struct drm_device *dev = obj->dev; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + /* + * As far as I can tell, writes of > 64 bytes will use non-temporal + * stores which should obviate the need for this clflush. + * It doesn't work for me though... + */ +/* if (size <= 64) */{ + if (obj_priv->gtt_space == NULL) { + int ret = i915_gem_object_get_page_list (obj); + if (ret) + return ret; + } + i915_gem_clflush_object(obj); + if (obj_priv->gtt_space == NULL) + i915_gem_object_free_page_list (obj); + } + drm_agp_chipset_flush(dev); + return 0; +} + void i915_gem_lastclose(struct drm_device *dev) { -- cgit v1.2.3 From 6950b7da7157d4d775fc09c7bc31fb0e769c4249 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 12 May 2008 13:00:55 -0700 Subject: [gem] Clarify use of explicit domain control. Remove Gen3 from I-cache usage. --- linux-core/drm-gem.txt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm-gem.txt b/linux-core/drm-gem.txt index 8f95c206..bef437d8 100644 --- a/linux-core/drm-gem.txt +++ b/linux-core/drm-gem.txt @@ -358,6 +358,12 @@ to be generally true, this ioctl may be simplified further. necessary CPU flushing will occur and the object will be correctly synchronized with the GPU. + Note that this synchronization is not required for any accesses + going through the driver itself. The pread, pwrite and execbuffer + ioctls all perform the necessary domain management internally. + Explicit synchronization is only necessary when accessing the object + through the mmap'd address. + 7. Execution (Intel specific) Managing the command buffers is inherently chip-specific, so the core of gem @@ -475,9 +481,9 @@ to synchronize what is needed while leaving other cache contents intact. needs to be flushed to the GPU. * DRM_GEM_DOMAIN_I915_INSTRUCTION - Fragment programs on Gen3 and all of the programs on later - chips use an instruction cache to speed program execution. It must be - explicitly flushed when new programs are written to memory by the CPU. + All of the programs on Gen4 and later chips use an instruction cache to + speed program execution. It must be explicitly flushed when new programs + are written to memory by the CPU. * DRM_GEM_DOMAIN_I915_VERTEX Vertex data uses two different vertex caches, but they're -- cgit v1.2.3 From 6aeff6b9e32b61beed0d5c3d871968fa6217b79d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 12 May 2008 13:01:57 -0700 Subject: [gem] Set write domain to CPU when doing pwrite. Leave the flush call in place, which can fix domains up if necessary. --- linux-core/drm_gem.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 14cf7e47..e20cfe07 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -276,9 +276,9 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, return -EINVAL; if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain (obj, - DRM_GEM_DOMAIN_CPU, - 0); + ret = dev->driver->gem_set_domain(obj, + DRM_GEM_DOMAIN_CPU, + 0); if (ret) { drm_gem_object_unreference(obj); return ret; @@ -363,9 +363,9 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, return -EINVAL; if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain (obj, - DRM_GEM_DOMAIN_CPU, - 0); + ret = dev->driver->gem_set_domain(obj, + DRM_GEM_DOMAIN_CPU, + DRM_GEM_DOMAIN_CPU); if (ret) { drm_gem_object_unreference(obj); return ret; @@ -376,7 +376,7 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, written = vfs_write(obj->filp, (char __user *)(uintptr_t) args->data_ptr, args->size, &offset); - + if (written != args->size) { drm_gem_object_unreference(obj); if (written < 0) -- cgit v1.2.3 From 17e8000ac046e912bf02649e67165cafed270e2e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 12 May 2008 13:04:18 -0700 Subject: [intel] Minor kludge -- wait for the ring to be nearly empty before queuing No need to fill the ring that much; wait for it to become nearly empty before adding the execbuffer request. A better fix will involve scheduling ring insertion in the irq handler. --- linux-core/i915_gem.c | 67 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 18 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 9d701ba1..90332bf0 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -463,9 +463,9 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) DRM_INFO("Binding object of size %d at 0x%08x\n", obj->size, obj_priv->gtt_offset); #endif - ret = i915_gem_object_get_page_list (obj); + ret = i915_gem_object_get_page_list(obj); if (ret) { - drm_memrange_put_block (obj_priv->gtt_space); + drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; return ret; } @@ -821,14 +821,43 @@ i915_dispatch_gem_execbuffer(struct drm_device *dev, return 0; } +/* + * Kludge -- wait for almost all rendering to complete + * before queuing more. This uses interrupts, so the wakeup + * occurs without any delay. + */ +static int +i915_gem_wait_space(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + drm_i915_ring_buffer_t *ring = &(dev_priv->ring); + struct drm_i915_gem_object *obj_priv, *last_priv = NULL; + int ret = 0; + + while (ring->space + 1024 < dev_priv->ring.Size && + !list_empty(&dev_priv->mm.execution_list)) { + obj_priv = list_first_entry(&dev_priv->mm.execution_list, + struct drm_i915_gem_object, + gtt_lru_entry); + if (obj_priv == last_priv) + break; + ret = i915_gem_object_wait_rendering(obj_priv->obj); + if (ret) + break; + last_priv = obj_priv; + i915_kernel_lost_context(dev); + } + return ret; +} + int i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) { drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_execbuffer *args = data; - struct drm_i915_gem_exec_object *validate_list; - struct drm_gem_object **object_list; + struct drm_i915_gem_exec_object *validate_list = NULL; + struct drm_gem_object **object_list = NULL; struct drm_gem_object *batch_obj; int ret, i; uint64_t exec_offset; @@ -842,6 +871,10 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, #endif i915_kernel_lost_context(dev); + ret = i915_gem_wait_space(dev); + if (ret) + return ret; + /* Copy in the validate list from userland */ validate_list = drm_calloc(sizeof(*validate_list), args->buffer_count, DRM_MEM_DRIVER); @@ -975,7 +1008,6 @@ err: for (i = 0; i < args->buffer_count; i++) drm_gem_object_unreference(object_list[i]); } - drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, @@ -1078,22 +1110,21 @@ i915_gem_flush_pwrite(struct drm_gem_object *obj, { struct drm_device *dev = obj->dev; struct drm_i915_gem_object *obj_priv = obj->driver_private; + /* - * As far as I can tell, writes of > 64 bytes will use non-temporal - * stores which should obviate the need for this clflush. - * It doesn't work for me though... + * For writes much less than the size of the object and + * which are already pinned in memory, do the flush right now */ -/* if (size <= 64) */{ - if (obj_priv->gtt_space == NULL) { - int ret = i915_gem_object_get_page_list (obj); - if (ret) - return ret; - } - i915_gem_clflush_object(obj); - if (obj_priv->gtt_space == NULL) - i915_gem_object_free_page_list (obj); + + if ((size < obj->size >> 1) && obj_priv->page_list != NULL) { + unsigned long first_page = offset / PAGE_SIZE; + unsigned long beyond_page = roundup(offset + size, PAGE_SIZE); + + drm_ttm_cache_flush(obj_priv->page_list + first_page, + beyond_page - first_page); + drm_agp_chipset_flush(dev); + obj->write_domain = 0; } - drm_agp_chipset_flush(dev); return 0; } -- cgit v1.2.3 From f650d7240a5b6eea8e605734f1211c20727c21d7 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 12 May 2008 12:55:36 -0700 Subject: [GEM] Typo (and thinking) fixes in drm-gem.txt and doxygen. --- linux-core/drm-gem.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm-gem.txt b/linux-core/drm-gem.txt index bef437d8..5cda87f8 100644 --- a/linux-core/drm-gem.txt +++ b/linux-core/drm-gem.txt @@ -50,7 +50,7 @@ subsystems, it does this with a modest amount of code. 2. API Overview and Conventions All APIs here are defined in terms of ioctls appplied to the DRM file -descriptor. To create and manipulate objects, an applications must be +descriptor. To create and manipulate objects, an application must be 'authorized' using the DRI or DRI2 protocols with the X server. To relax that, we will need to implement some better access control mechanisms within the hardware portion of the driver to prevent inappropriate @@ -384,15 +384,15 @@ target-object relative value into the object. The Intel driver has a single relocation type: struct drm_i915_gem_relocation_entry { - /* + /** * Handle of the buffer being pointed to by this * relocation entry. - /* + * * It's appealing to make this be an index into the - * mm_validate_entry list to refer to the buffer, but - * handle lookup should be O(1) anyway, and prevents - * O(n) search in userland to find what that index is. - + * mm_validate_entry list to refer to the buffer, + * but this allows the driver to create a relocation + * list for state buffers and not re-write it per + * exec using the buffer. */ uint32_t target_handle; @@ -545,7 +545,7 @@ to synchronize what is needed while leaving other cache contents intact. struct drm_i915_gem_execbuffer { /** - * List of buffers to be validated wit their + * List of buffers to be validated with their * relocations to be performend on them. * * These buffers must be listed in an order such that -- cgit v1.2.3 From aafafe507ba6d073bad25b74c808b73c428db67f Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 15 May 2008 09:32:34 -0700 Subject: [gem] typo fix in comment. --- linux-core/drm_gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index e20cfe07..ff1a38dc 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -61,7 +61,7 @@ * DRM terminology) to mimic fds, and implement the fd syscalls we need as * ioctls. The objects themselves will still include the struct file so * that we can transition to fds if the required kernel infrastructure shows - * up at a later data, and as our interface with shmfs for memory allocation. + * up at a later date, and as our interface with shmfs for memory allocation. */ /** -- cgit v1.2.3 From 3ab152da66f6c7bcc68a13efcf4a62800354f13b Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 15 May 2008 09:37:49 -0700 Subject: [gem] Rename the GTT LRU lists to active (executing) and inactive (idle). --- linux-core/i915_gem.c | 54 +++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 90332bf0..00f56013 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -170,8 +170,8 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) drm_gem_object_reference(obj); /* Move from whatever list we were on to the tail of execution. */ - list_move_tail(&obj_priv->gtt_lru_entry, - &dev_priv->mm.execution_list); + list_move_tail(&obj_priv->list, + &dev_priv->mm.active_list); obj_priv->last_rendering_cookie = i915_emit_irq(dev); BUG_ON(obj_priv->last_rendering_cookie == 0); #if WATCH_LRU @@ -197,8 +197,8 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) * Move to the tail of the LRU list now since we're done. */ if (obj_priv->pin_count == 0) - list_move_tail(&obj_priv->gtt_lru_entry, - &dev_priv->mm.gtt_lru); + list_move_tail(&obj_priv->list, + &dev_priv->mm.inactive_list); #if WATCH_LRU DRM_INFO("%s: wait moves to lru list %p\n", __func__, obj); @@ -245,8 +245,8 @@ i915_gem_object_unbind(struct drm_gem_object *obj) obj_priv->gtt_space = NULL; /* Remove ourselves from the LRU list if present. */ - if (!list_empty(&obj_priv->gtt_lru_entry)) { - list_del_init(&obj_priv->gtt_lru_entry); + if (!list_empty(&obj_priv->list)) { + list_del_init(&obj_priv->list); if (obj_priv->last_rendering_cookie) { DRM_ERROR("Failed to wait on buffer when unbinding, " "continued anyway.\n"); @@ -309,14 +309,14 @@ i915_dump_lru(struct drm_device *dev, const char *where) struct drm_i915_gem_object *obj_priv; DRM_INFO("GTT execution list %s {\n", where); - list_for_each_entry(obj_priv, &dev_priv->mm.execution_list, - gtt_lru_entry) + list_for_each_entry(obj_priv, &dev_priv->mm.active_list, + list) { DRM_INFO(" %p: %08x\n", obj_priv, obj_priv->last_rendering_cookie); } DRM_INFO("GTT LRU %s {\n", where); - list_for_each_entry(obj_priv, &dev_priv->mm.gtt_lru, gtt_lru_entry) { + list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { DRM_INFO(" %p: %08x\n", obj_priv, obj_priv->last_rendering_cookie); } @@ -333,16 +333,16 @@ i915_gem_evict_something(struct drm_device *dev) int ret; /* Find the LRU buffer. */ - if (!list_empty(&dev_priv->mm.gtt_lru)) { - obj_priv = list_first_entry(&dev_priv->mm.gtt_lru, + if (!list_empty(&dev_priv->mm.inactive_list)) { + obj_priv = list_first_entry(&dev_priv->mm.inactive_list, struct drm_i915_gem_object, - gtt_lru_entry); - } else if (!list_empty(&dev_priv->mm.execution_list)) { + list); + } else if (!list_empty(&dev_priv->mm.active_list)) { /* If there's nothing unused and ready, grab the first * unpinned object from the currently executing list. */ - list_for_each_entry(obj_priv, &dev_priv->mm.execution_list, - gtt_lru_entry) + list_for_each_entry(obj_priv, &dev_priv->mm.active_list, + list) if (obj_priv->pin_count == 0) break; if (!obj_priv) @@ -447,8 +447,8 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) #if WATCH_LRU DRM_INFO("%s: GTT full, evicting something\n", __func__); #endif - if (list_empty(&dev_priv->mm.gtt_lru) && - list_empty(&dev_priv->mm.execution_list)) { + if (list_empty(&dev_priv->mm.inactive_list) && + list_empty(&dev_priv->mm.active_list)) { DRM_ERROR("GTT full, but LRU list empty\n"); return -ENOMEM; } @@ -835,10 +835,10 @@ i915_gem_wait_space(struct drm_device *dev) int ret = 0; while (ring->space + 1024 < dev_priv->ring.Size && - !list_empty(&dev_priv->mm.execution_list)) { - obj_priv = list_first_entry(&dev_priv->mm.execution_list, + !list_empty(&dev_priv->mm.active_list)) { + obj_priv = list_first_entry(&dev_priv->mm.active_list, struct drm_i915_gem_object, - gtt_lru_entry); + list); if (obj_priv == last_priv) break; ret = i915_gem_object_wait_rendering(obj_priv->obj); @@ -984,8 +984,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, obj_priv->last_rendering_cookie = cookie; BUG_ON(obj_priv->last_rendering_cookie == 0); /* Move our buffer to the tail of the execution list. */ - list_move_tail(&obj_priv->gtt_lru_entry, - &dev_priv->mm.execution_list); + list_move_tail(&obj_priv->list, + &dev_priv->mm.active_list); #if WATCH_LRU DRM_INFO("%s: move to exec list %p\n", __func__, obj); #endif @@ -1083,7 +1083,7 @@ int i915_gem_init_object(struct drm_gem_object *obj) obj->driver_private = obj_priv; obj_priv->obj = obj; - INIT_LIST_HEAD(&obj_priv->gtt_lru_entry); + INIT_LIST_HEAD(&obj_priv->list); return 0; } @@ -1138,14 +1138,14 @@ i915_gem_lastclose(struct drm_device *dev) * close, this is also the last ref and they'll go away. */ - while (!list_empty(&dev_priv->mm.execution_list)) { + while (!list_empty(&dev_priv->mm.active_list)) { struct drm_i915_gem_object *obj_priv; - obj_priv = list_first_entry(&dev_priv->mm.execution_list, + obj_priv = list_first_entry(&dev_priv->mm.active_list, struct drm_i915_gem_object, - gtt_lru_entry); + list); - list_del_init(&obj_priv->gtt_lru_entry); + list_del_init(&obj_priv->list); obj_priv->last_rendering_cookie = 0; obj_priv->obj->write_domain = 0; drm_gem_object_unreference(obj_priv->obj); -- cgit v1.2.3 From 7dced2f33a952ad12aafb7a3e34747156020a3ae Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 15 May 2008 11:21:11 -0700 Subject: [gem] Hold dev->struct_mutex to protect structure data. --- linux-core/drm_gem.c | 26 ++++++++++++++++++++++++++ linux-core/i915_drv.c | 2 +- linux-core/i915_gem.c | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index ff1a38dc..a8253bc5 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -116,6 +116,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) static int drm_gem_handle_delete(struct drm_file *filp, int handle) { + struct drm_device *dev; struct drm_gem_object *obj; /* This is gross. The idr system doesn't let us try a delete and @@ -135,12 +136,15 @@ drm_gem_handle_delete(struct drm_file *filp, int handle) spin_unlock(&filp->table_lock); return -EINVAL; } + dev = obj->dev; /* Release reference and decrement refcount. */ idr_remove(&filp->object_idr, handle); spin_unlock(&filp->table_lock); + mutex_lock(&dev->struct_mutex); drm_gem_object_handle_unreference(obj); + mutex_unlock(&dev->struct_mutex); return 0; } @@ -225,7 +229,9 @@ drm_gem_create_ioctl(struct drm_device *dev, void *data, return -ENOMEM; ret = drm_gem_handle_create(file_priv, obj, &handle); + mutex_lock(&dev->struct_mutex); drm_gem_object_handle_unreference(obj); + mutex_unlock(&dev->struct_mutex); if (ret) return ret; @@ -275,12 +281,14 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, if (obj == NULL) return -EINVAL; + mutex_lock(&dev->struct_mutex); if (dev->driver->gem_set_domain) { ret = dev->driver->gem_set_domain(obj, DRM_GEM_DOMAIN_CPU, 0); if (ret) { drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return ret; } } @@ -290,6 +298,7 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, args->size, &offset); if (read != args->size) { drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); if (read < 0) return read; else @@ -297,6 +306,7 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, } drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return 0; } @@ -331,7 +341,9 @@ drm_gem_mmap_ioctl(struct drm_device *dev, void *data, PROT_READ | PROT_WRITE, MAP_SHARED, args->offset); up_write(¤t->mm->mmap_sem); + mutex_lock(&dev->struct_mutex); drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); if (IS_ERR((void *)addr)) return addr; @@ -362,12 +374,14 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, if (obj == NULL) return -EINVAL; + mutex_lock(&dev->struct_mutex); if (dev->driver->gem_set_domain) { ret = dev->driver->gem_set_domain(obj, DRM_GEM_DOMAIN_CPU, DRM_GEM_DOMAIN_CPU); if (ret) { drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return ret; } } @@ -379,6 +393,7 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, if (written != args->size) { drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); if (written < 0) return written; else @@ -391,6 +406,7 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, args->size); drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return 0; } @@ -432,7 +448,9 @@ again: goto again; if (ret != 0) { + mutex_lock(&dev->struct_mutex); drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return ret; } @@ -472,7 +490,9 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data, return -ENOENT; ret = drm_gem_handle_create(file_priv, obj, &handle); + mutex_lock(&dev->struct_mutex); drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); if (ret) return ret; @@ -500,6 +520,7 @@ drm_gem_set_domain_ioctl(struct drm_device *dev, void *data, if (obj == NULL) return -EINVAL; + mutex_lock(&dev->struct_mutex); if (dev->driver->gem_set_domain) { ret = dev->driver->gem_set_domain(obj, args->read_domains, @@ -510,6 +531,7 @@ drm_gem_set_domain_ioctl(struct drm_device *dev, void *data, ret = 0; } drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return ret; } @@ -546,10 +568,12 @@ drm_gem_object_release_handle(int id, void *ptr, void *data) void drm_gem_release(struct drm_device *dev, struct drm_file *file_private) { + mutex_lock(&dev->struct_mutex); idr_for_each(&file_private->object_idr, &drm_gem_object_release_handle, NULL); idr_destroy(&file_private->object_idr); + mutex_unlock(&dev->struct_mutex); } /** @@ -563,6 +587,8 @@ drm_gem_object_free(struct kref *kref) struct drm_gem_object *obj = (struct drm_gem_object *) kref; struct drm_device *dev = obj->dev; + BUG_ON(!mutex_is_locked(&dev->struct_mutex)); + if (dev->driver->gem_free_object != NULL) dev->driver->gem_free_object(obj); diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index cc47ed64..a50ee53c 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -588,7 +588,7 @@ static struct drm_driver driver = { .ioctls = i915_ioctls, .gem_init_object = i915_gem_init_object, .gem_free_object = i915_gem_free_object, - .gem_set_domain = i915_gem_set_domain_ioctl, + .gem_set_domain = i915_gem_set_domain, .gem_flush_pwrite = i915_gem_flush_pwrite, .fops = { .owner = THIS_MODULE, diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 00f56013..144667a3 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -42,14 +42,20 @@ i915_gem_init_ioctl(struct drm_device *dev, void *data, drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_init *args = data; + mutex_lock(&dev->struct_mutex); + if (args->gtt_start >= args->gtt_end || (args->gtt_start & (PAGE_SIZE - 1)) != 0 || - (args->gtt_end & (PAGE_SIZE - 1)) != 0) + (args->gtt_end & (PAGE_SIZE - 1)) != 0) { + mutex_unlock(&dev->struct_mutex); return -EINVAL; + } drm_memrange_init(&dev_priv->mm.gtt_space, args->gtt_start, args->gtt_end - args->gtt_start); + mutex_unlock(&dev->struct_mutex); + return 0; } @@ -834,6 +840,7 @@ i915_gem_wait_space(struct drm_device *dev) struct drm_i915_gem_object *obj_priv, *last_priv = NULL; int ret = 0; + mutex_lock(&dev->struct_mutex); while (ring->space + 1024 < dev_priv->ring.Size && !list_empty(&dev_priv->mm.active_list)) { obj_priv = list_first_entry(&dev_priv->mm.active_list, @@ -847,6 +854,7 @@ i915_gem_wait_space(struct drm_device *dev) last_priv = obj_priv; i915_kernel_lost_context(dev); } + mutex_unlock(&dev->struct_mutex); return ret; } @@ -897,6 +905,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } + mutex_lock(&dev->struct_mutex); /* Look up object handles and perform the relocations */ for (i = 0; i < args->buffer_count; i++) { object_list[i] = drm_gem_object_lookup(dev, file_priv, @@ -1008,6 +1017,8 @@ err: for (i = 0; i < args->buffer_count; i++) drm_gem_object_unreference(object_list[i]); } + mutex_unlock(&dev->struct_mutex); + drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, @@ -1025,10 +1036,13 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, struct drm_i915_gem_object *obj_priv; int ret; + mutex_lock(&dev->struct_mutex); + obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) { DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n", args->handle); + mutex_unlock(&dev->struct_mutex); return -EINVAL; } @@ -1041,6 +1055,7 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, "i915_gem_pin_ioctl(): %d\n", ret); drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return ret; } } @@ -1048,6 +1063,7 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, obj_priv->pin_count++; args->offset = obj_priv->gtt_offset; drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return 0; } @@ -1060,16 +1076,19 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, struct drm_gem_object *obj; struct drm_i915_gem_object *obj_priv; + mutex_lock(&dev->struct_mutex); obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) { DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n", args->handle); + mutex_unlock(&dev->struct_mutex); return -EINVAL; } obj_priv = obj->driver_private; obj_priv->pin_count--; drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); return 0; } @@ -1095,12 +1114,17 @@ void i915_gem_free_object(struct drm_gem_object *obj) } int -i915_gem_set_domain_ioctl(struct drm_gem_object *obj, - uint32_t read_domains, - uint32_t write_domain) +i915_gem_set_domain(struct drm_gem_object *obj, + uint32_t read_domains, + uint32_t write_domain) { + struct drm_device *dev = obj->dev; + + BUG_ON(!mutex_is_locked(&dev->struct_mutex)); + i915_gem_object_set_domain(obj, read_domains, write_domain); i915_gem_dev_set_domain(obj->dev); + return 0; } @@ -1133,6 +1157,8 @@ i915_gem_lastclose(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; + mutex_lock(&dev->struct_mutex); + /* Assume that the chip has been idled at this point. Just pull them * off the execution list and unref them. Since this is the last * close, this is also the last ref and they'll go away. @@ -1150,4 +1176,6 @@ i915_gem_lastclose(struct drm_device *dev) obj_priv->obj->write_domain = 0; drm_gem_object_unreference(obj_priv->obj); } + + mutex_unlock(&dev->struct_mutex); } -- cgit v1.2.3 From 6c3ac484b049681f9f3e692f9a6238ed122a8191 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 20 May 2008 10:48:36 -0700 Subject: [gem] Clean up active/inactive list handling using helper functions. Additionally, a boolean active field is added to indicate which list an object is on, rather than smashing last_rendering_cookie to 0 to show inactive. This will help with flush-reduction later on, and makes the code clearer. --- linux-core/i915_gem.c | 80 ++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 144667a3..1a97fdef 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -80,6 +80,42 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) obj_priv->page_list = NULL; } +static void +i915_gem_object_move_to_active(struct drm_gem_object *obj) +{ + struct drm_device *dev = obj->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + /* Add a reference if we're newly entering the active list. */ + if (!obj_priv->active) { + drm_gem_object_reference(obj); + obj_priv->active = 1; + } + /* Move from whatever list we were on to the tail of execution. */ + list_move_tail(&obj_priv->list, + &dev_priv->mm.active_list); +} + +static void +i915_gem_object_move_to_inactive(struct drm_gem_object *obj) +{ + struct drm_device *dev = obj->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + if (obj_priv->pin_count != 0) + list_del_init(&obj_priv->list); + else + list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list); + + if (obj_priv->active) { + obj_priv->active = 0; + drm_gem_object_unreference(obj); + } +} + + static void i915_gem_flush(struct drm_device *dev, uint32_t invalidate_domains, @@ -156,7 +192,6 @@ static int i915_gem_object_wait_rendering(struct drm_gem_object *obj) { struct drm_device *dev = obj->dev; - drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; int ret; @@ -171,13 +206,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) i915_gem_flush(dev, 0, obj->write_domain); obj->write_domain = 0; - /* Add a reference since we're gaining a cookie. */ - if (obj_priv->last_rendering_cookie == 0) - drm_gem_object_reference(obj); - /* Move from whatever list we were on to the tail of execution. - */ - list_move_tail(&obj_priv->list, - &dev_priv->mm.active_list); + i915_gem_object_move_to_active(obj); obj_priv->last_rendering_cookie = i915_emit_irq(dev); BUG_ON(obj_priv->last_rendering_cookie == 0); #if WATCH_LRU @@ -187,7 +216,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) /* If there is rendering queued on the buffer being evicted, wait for * it. */ - if (obj_priv->last_rendering_cookie != 0) { + if (obj_priv->active) { #if WATCH_BUF DRM_INFO("%s: object %p wait for cookie %08x\n", __func__, obj, obj_priv->last_rendering_cookie); @@ -196,23 +225,11 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) if (ret != 0) return ret; - /* Clear it now that we know it's passed. */ - obj_priv->last_rendering_cookie = 0; - - /* We were on the execution list since we had a cookie. - * Move to the tail of the LRU list now since we're done. - */ - if (obj_priv->pin_count == 0) - list_move_tail(&obj_priv->list, - &dev_priv->mm.inactive_list); + i915_gem_object_move_to_inactive(obj); #if WATCH_LRU DRM_INFO("%s: wait moves to lru list %p\n", __func__, obj); #endif - /* The cookie held a reference to the object, release that - * now - */ - drm_gem_object_unreference(obj); } return 0; @@ -253,10 +270,10 @@ i915_gem_object_unbind(struct drm_gem_object *obj) /* Remove ourselves from the LRU list if present. */ if (!list_empty(&obj_priv->list)) { list_del_init(&obj_priv->list); - if (obj_priv->last_rendering_cookie) { + if (obj_priv->active) { DRM_ERROR("Failed to wait on buffer when unbinding, " "continued anyway.\n"); - obj_priv->last_rendering_cookie = 0; + obj_priv->active = 0; drm_gem_object_unreference(obj); } } @@ -862,7 +879,6 @@ int i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) { - drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_execbuffer *args = data; struct drm_i915_gem_exec_object *validate_list = NULL; struct drm_gem_object **object_list = NULL; @@ -980,21 +996,13 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, * wait on when trying to clear up gtt space). */ cookie = i915_emit_irq(dev); + BUG_ON(cookie == 0); for (i = 0; i < args->buffer_count; i++) { struct drm_gem_object *obj = object_list[i]; struct drm_i915_gem_object *obj_priv = obj->driver_private; - /* - * Have the cookie hold a reference to this object - * which is freed when the object is waited for - */ - if (obj_priv->last_rendering_cookie == 0) - drm_gem_object_reference(obj); + i915_gem_object_move_to_active(obj); obj_priv->last_rendering_cookie = cookie; - BUG_ON(obj_priv->last_rendering_cookie == 0); - /* Move our buffer to the tail of the execution list. */ - list_move_tail(&obj_priv->list, - &dev_priv->mm.active_list); #if WATCH_LRU DRM_INFO("%s: move to exec list %p\n", __func__, obj); #endif @@ -1172,7 +1180,7 @@ i915_gem_lastclose(struct drm_device *dev) list); list_del_init(&obj_priv->list); - obj_priv->last_rendering_cookie = 0; + obj_priv->active = 0; obj_priv->obj->write_domain = 0; drm_gem_object_unreference(obj_priv->obj); } -- cgit v1.2.3 From ab36a6f983107971890e81473452b3f0313fb692 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 20 May 2008 10:52:24 -0700 Subject: [gem] Rename sequence numbers from "cookie" to "seqno" --- linux-core/i915_gem.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 1a97fdef..ee8f1023 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -196,7 +196,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) int ret; /* If there are writes queued to the buffer, flush and - * create a new cookie to wait for. + * create a new seqno to wait for. */ if (obj->write_domain & ~(DRM_GEM_DOMAIN_CPU)) { #if WATCH_BUF @@ -207,8 +207,8 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) obj->write_domain = 0; i915_gem_object_move_to_active(obj); - obj_priv->last_rendering_cookie = i915_emit_irq(dev); - BUG_ON(obj_priv->last_rendering_cookie == 0); + obj_priv->last_rendering_seqno = i915_emit_irq(dev); + BUG_ON(obj_priv->last_rendering_seqno == 0); #if WATCH_LRU DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj); #endif @@ -218,10 +218,10 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) */ if (obj_priv->active) { #if WATCH_BUF - DRM_INFO("%s: object %p wait for cookie %08x\n", - __func__, obj, obj_priv->last_rendering_cookie); + DRM_INFO("%s: object %p wait for seqno %08x\n", + __func__, obj, obj_priv->last_rendering_seqno); #endif - ret = i915_wait_irq(dev, obj_priv->last_rendering_cookie); + ret = i915_wait_irq(dev, obj_priv->last_rendering_seqno); if (ret != 0) return ret; @@ -336,12 +336,12 @@ i915_dump_lru(struct drm_device *dev, const char *where) list) { DRM_INFO(" %p: %08x\n", obj_priv, - obj_priv->last_rendering_cookie); + obj_priv->last_rendering_seqno); } DRM_INFO("GTT LRU %s {\n", where); list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { DRM_INFO(" %p: %08x\n", obj_priv, - obj_priv->last_rendering_cookie); + obj_priv->last_rendering_seqno); } DRM_INFO("}\n"); } @@ -885,7 +885,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_gem_object *batch_obj; int ret, i; uint64_t exec_offset; - uint32_t cookie; + uint32_t seqno; LOCK_TEST_WITH_RETURN(dev, file_priv); @@ -989,20 +989,20 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } /* - * Get a cookie representing the execution of the current buffer, + * Get a seqno representing the execution of the current buffer, * which we can wait on. We would like to mitigate these interrupts, - * likely by only creating cookies occasionally (so that we have + * likely by only creating seqnos occasionally (so that we have * *some* interrupts representing completion of buffers that we can * wait on when trying to clear up gtt space). */ - cookie = i915_emit_irq(dev); - BUG_ON(cookie == 0); + seqno = i915_emit_irq(dev); + BUG_ON(seqno == 0); for (i = 0; i < args->buffer_count; i++) { struct drm_gem_object *obj = object_list[i]; struct drm_i915_gem_object *obj_priv = obj->driver_private; i915_gem_object_move_to_active(obj); - obj_priv->last_rendering_cookie = cookie; + obj_priv->last_rendering_seqno = seqno; #if WATCH_LRU DRM_INFO("%s: move to exec list %p\n", __func__, obj); #endif -- cgit v1.2.3 From af8e087157ef5034fa12d93202037f87da61355d Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 20 May 2008 14:03:27 -0700 Subject: [gem] Use a separate sequence number field from classic/ttm This lets us get some qualities we desire, such as using the full 32-bit range (except zero), avoiding DRM_WAIT_ON, and a 1:1 mapping of active sequence numbers to request structs, which will be used soon for throttling and interrupt-driven list cleanup. --- linux-core/i915_gem.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index ee8f1023..3535fae1 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -115,6 +115,123 @@ i915_gem_object_move_to_inactive(struct drm_gem_object *obj) } } +/** + * Creates a new sequence number, emitting a write of it to the status page + * plus an interrupt, which will trigger i915_user_interrupt_handler. + * + * Must be called with struct_lock held. + * + * Returned sequence numbers are nonzero on success. + */ +static uint32_t +i915_add_request(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_request *request; + uint32_t seqno; + RING_LOCALS; + + request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER); + if (request == NULL) + return 0; + + /* Grab the seqno we're going to make this request be, and bump the + * next (skipping 0 so it can be the reserved no-seqno value). + */ + seqno = dev_priv->mm.next_gem_seqno; + dev_priv->mm.next_gem_seqno++; + if (dev_priv->mm.next_gem_seqno == 0) + dev_priv->mm.next_gem_seqno++; + + BEGIN_LP_RING(4); + OUT_RING(CMD_STORE_DWORD_IDX); + OUT_RING(I915_GEM_HWS_INDEX << STORE_DWORD_INDEX_SHIFT); + OUT_RING(seqno); + + OUT_RING(GFX_OP_USER_INTERRUPT); + ADVANCE_LP_RING(); + + DRM_DEBUG("%d\n", seqno); + + request->seqno = seqno; + list_add_tail(&request->list, &dev_priv->mm.request_list); + + return seqno; +} + +/** + * Returns true if seq1 is later than seq2. + */ +static int +i915_seqno_passed(uint32_t seq1, uint32_t seq2) +{ + return (int32_t)(seq1 - seq2) >= 0; +} + +static uint32_t +i915_get_gem_seqno(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + + return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX); +} + +/** + * This function clears the request list as sequence numbers are passed. + */ +void +i915_gem_retire_requests(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + uint32_t seqno; + + seqno = i915_get_gem_seqno(dev); + + while (!list_empty(&dev_priv->mm.request_list)) { + struct drm_i915_gem_request *request; + uint32_t retiring_seqno; + + request = list_first_entry(&dev_priv->mm.request_list, + struct drm_i915_gem_request, + list); + retiring_seqno = request->seqno; + + if (i915_seqno_passed(seqno, retiring_seqno)) { + list_del(&request->list); + drm_free(request, sizeof(*request), DRM_MEM_DRIVER); + } else + break; + } +} + +/** + * Waits for a sequence number to be signaled, and cleans up the + * request and object lists appropriately for that event. + */ +int +i915_wait_request(struct drm_device *dev, uint32_t seqno) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + int ret = 0; + + BUG_ON(seqno == 0); + + i915_user_irq_on(dev_priv); + ret = wait_event_interruptible(dev_priv->irq_queue, + i915_seqno_passed(i915_get_gem_seqno(dev), + seqno)); + i915_user_irq_off(dev_priv); + + /* Directly dispatch request retiring. While we have the work queue + * to handle this, the waiter on a request often wants an associated + * buffer to have made it to the inactive list, and we would need + * a separate wait queue to handle that. + */ + if (ret == 0) + i915_gem_retire_requests(dev); + + return ret; +} static void i915_gem_flush(struct drm_device *dev, @@ -207,7 +324,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) obj->write_domain = 0; i915_gem_object_move_to_active(obj); - obj_priv->last_rendering_seqno = i915_emit_irq(dev); + obj_priv->last_rendering_seqno = i915_add_request(dev); BUG_ON(obj_priv->last_rendering_seqno == 0); #if WATCH_LRU DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj); @@ -221,7 +338,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) DRM_INFO("%s: object %p wait for seqno %08x\n", __func__, obj, obj_priv->last_rendering_seqno); #endif - ret = i915_wait_irq(dev, obj_priv->last_rendering_seqno); + ret = i915_wait_request(dev, obj_priv->last_rendering_seqno); if (ret != 0) return ret; @@ -995,7 +1112,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, * *some* interrupts representing completion of buffers that we can * wait on when trying to clear up gtt space). */ - seqno = i915_emit_irq(dev); + seqno = i915_add_request(dev); BUG_ON(seqno == 0); for (i = 0; i < args->buffer_count; i++) { struct drm_gem_object *obj = object_list[i]; -- cgit v1.2.3 From f8e38e49dd70bf4a5ef97c29f0c405a1fc5023f5 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 21 May 2008 00:32:02 -0700 Subject: [intel-gem] invalidate ring locals for pin/unpin/set_domain/free functions Ring locals must be reloaded from hardware in case the X server ran. --- linux-core/i915_gem.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 3535fae1..8e5539f5 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1163,6 +1163,7 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); + i915_kernel_lost_context(dev); obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) { DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n", @@ -1202,6 +1203,8 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, struct drm_i915_gem_object *obj_priv; mutex_lock(&dev->struct_mutex); + + i915_kernel_lost_context(dev); obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) { DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n", @@ -1233,6 +1236,7 @@ int i915_gem_init_object(struct drm_gem_object *obj) void i915_gem_free_object(struct drm_gem_object *obj) { + i915_kernel_lost_context(obj->dev); i915_gem_object_unbind(obj); drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); @@ -1247,6 +1251,7 @@ i915_gem_set_domain(struct drm_gem_object *obj, BUG_ON(!mutex_is_locked(&dev->struct_mutex)); + i915_kernel_lost_context(dev); i915_gem_object_set_domain(obj, read_domains, write_domain); i915_gem_dev_set_domain(obj->dev); -- cgit v1.2.3 From 7078978db0e014a2621984f6c67ca65fa4f23f3a Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 21 May 2008 15:03:47 -0700 Subject: [gem] Hold a reference on the object in i915_gem_wait_space. Otherwise, in the middle of the function called using it the last ref might disappear. --- linux-core/i915_gem.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 8e5539f5..e01823fb 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -982,7 +982,9 @@ i915_gem_wait_space(struct drm_device *dev) list); if (obj_priv == last_priv) break; + drm_gem_object_reference(obj_priv->obj); ret = i915_gem_object_wait_rendering(obj_priv->obj); + drm_gem_object_unreference(obj_priv->obj); if (ret) break; last_priv = obj_priv; -- cgit v1.2.3 From 54fa32cdfe1529023324a0a261ee5d4e033f46ea Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 21 May 2008 15:15:58 -0700 Subject: [gem] Fix bad test for list_for_each completion. Since it's a circular list, the entry won't be NULL at termination. --- linux-core/i915_gem.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index e01823fb..94e2e477 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -478,14 +478,19 @@ i915_gem_evict_something(struct drm_device *dev) struct drm_i915_gem_object, list); } else if (!list_empty(&dev_priv->mm.active_list)) { + int found = 0; + /* If there's nothing unused and ready, grab the first * unpinned object from the currently executing list. */ list_for_each_entry(obj_priv, &dev_priv->mm.active_list, - list) - if (obj_priv->pin_count == 0) + list) { + if (obj_priv->pin_count == 0) { + found = 1; break; - if (!obj_priv) + } + } + if (!found) return -ENOMEM; } else { return -ENOMEM; -- cgit v1.2.3 From d6f796857780fc54641047e2aa4e7091376928eb Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 20 May 2008 16:27:05 -0700 Subject: [gem] Replace ring throttling hack with actual time measurement. --- linux-core/i915_gem.c | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 94e2e477..641ca8a3 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -154,6 +154,7 @@ i915_add_request(struct drm_device *dev) DRM_DEBUG("%d\n", seqno); request->seqno = seqno; + request->emitted_jiffies = jiffies; list_add_tail(&request->list, &dev_priv->mm.request_list); return seqno; @@ -966,34 +967,38 @@ i915_dispatch_gem_execbuffer(struct drm_device *dev, return 0; } -/* - * Kludge -- wait for almost all rendering to complete - * before queuing more. This uses interrupts, so the wakeup - * occurs without any delay. +/* Throttle our rendering by waiting until the ring has completed our requests + * emitted over 20 msec ago. + * + * This should get us reasonable parallelism between CPU and GPU but also + * relatively low latency when blocking on a particular request to finish. */ static int -i915_gem_wait_space(struct drm_device *dev) +i915_gem_ring_throttle(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; - drm_i915_ring_buffer_t *ring = &(dev_priv->ring); - struct drm_i915_gem_object *obj_priv, *last_priv = NULL; int ret = 0; mutex_lock(&dev->struct_mutex); - while (ring->space + 1024 < dev_priv->ring.Size && - !list_empty(&dev_priv->mm.active_list)) { - obj_priv = list_first_entry(&dev_priv->mm.active_list, - struct drm_i915_gem_object, - list); - if (obj_priv == last_priv) - break; - drm_gem_object_reference(obj_priv->obj); - ret = i915_gem_object_wait_rendering(obj_priv->obj); - drm_gem_object_unreference(obj_priv->obj); - if (ret) - break; - last_priv = obj_priv; - i915_kernel_lost_context(dev); + while (!list_empty(&dev_priv->mm.request_list)) { + struct drm_i915_gem_request *request; + + request = list_first_entry(&dev_priv->mm.request_list, + struct drm_i915_gem_request, + list); + + /* Break out if we're close enough. */ + if (jiffies_to_msecs(jiffies - request->emitted_jiffies) < 20) { + mutex_unlock(&dev->struct_mutex); + return 0; + } + + /* Wait on the last request if not. */ + ret = i915_wait_request(dev, request->seqno); + if (ret != 0) { + mutex_unlock(&dev->struct_mutex); + return ret; + } } mutex_unlock(&dev->struct_mutex); return ret; @@ -1019,7 +1024,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, #endif i915_kernel_lost_context(dev); - ret = i915_gem_wait_space(dev); + ret = i915_gem_ring_throttle(dev); if (ret) return ret; -- cgit v1.2.3 From 5e662f90d1143de53db866e2b8a94f1bfbe5fc51 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 14 May 2008 15:13:14 -0700 Subject: [gem] Release GEM buffers from work task scheduled from IRQ. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are now 3 lists. Active is buffers currently in the ringbuffer. Flushing is not in the ringbuffer, but needs a flush before unbinding. Inactive is as before. This prevents object_free → unbind → wait_rendering → object_reference and a kernel oops about weird refcounting. This also avoids an synchronous extra flush and wait when freeing a buffer which had a write_domain set (such as a temporary rendered to and then from using the 2d engine). It will sit around on the flushing list until the appropriate flush gets emitted, or we need the GTT space for another operation. --- linux-core/i915_gem.c | 186 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 136 insertions(+), 50 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 641ca8a3..c67ce309 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -124,7 +124,7 @@ i915_gem_object_move_to_inactive(struct drm_gem_object *obj) * Returned sequence numbers are nonzero on success. */ static uint32_t -i915_add_request(struct drm_device *dev) +i915_add_request(struct drm_device *dev, uint32_t flush_domains) { drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_request *request; @@ -155,11 +155,73 @@ i915_add_request(struct drm_device *dev) request->seqno = seqno; request->emitted_jiffies = jiffies; + request->flush_domains = flush_domains; list_add_tail(&request->list, &dev_priv->mm.request_list); return seqno; } +/** + * Moves buffers associated only with the given active seqno from the active + * to inactive list, potentially freeing them. + */ +static void +i915_gem_retire_request(struct drm_device *dev, + struct drm_i915_gem_request *request) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + + if (request->flush_domains != 0) { + struct drm_i915_gem_object *obj_priv, *next; + + /* First clear any buffers that were only waiting for a flush + * matching the one just retired. + */ + + list_for_each_entry_safe(obj_priv, next, + &dev_priv->mm.flushing_list, list) { + struct drm_gem_object *obj = obj_priv->obj; + + if (obj->write_domain & request->flush_domains) { + obj->write_domain = 0; + i915_gem_object_move_to_inactive(obj); + } + } + + } + + /* Move any buffers on the active list that are no longer referenced + * by the ringbuffer to the flushing/inactive lists as appropriate. + */ + while (!list_empty(&dev_priv->mm.active_list)) { + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + + obj_priv = list_first_entry(&dev_priv->mm.active_list, + struct drm_i915_gem_object, + list); + obj = obj_priv->obj; + + /* If the seqno being retired doesn't match the oldest in the + * list, then the oldest in the list must still be newer than + * this seqno. + */ + if (obj_priv->last_rendering_seqno != request->seqno) + return; +#if WATCH_LRU + DRM_INFO("%s: retire %d moves to inactive list %p\n", + __func__, seqno, obj); +#endif + + if (obj->write_domain != 0) { + list_move_tail(&obj_priv->list, + &dev_priv->mm.flushing_list); + } else { + i915_gem_object_move_to_inactive(obj); + } + } +} + /** * Returns true if seq1 is later than seq2. */ @@ -198,6 +260,8 @@ i915_gem_retire_requests(struct drm_device *dev) retiring_seqno = request->seqno; if (i915_seqno_passed(seqno, retiring_seqno)) { + i915_gem_retire_request(dev, request); + list_del(&request->list); drm_free(request, sizeof(*request), DRM_MEM_DRIVER); } else @@ -317,15 +381,17 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) * create a new seqno to wait for. */ if (obj->write_domain & ~(DRM_GEM_DOMAIN_CPU)) { + uint32_t write_domain = obj->write_domain; #if WATCH_BUF DRM_INFO("%s: flushing object %p from write domain %08x\n", - __func__, obj, obj->write_domain); + __func__, obj, write_domain); #endif - i915_gem_flush(dev, 0, obj->write_domain); + i915_gem_flush(dev, 0, write_domain); obj->write_domain = 0; i915_gem_object_move_to_active(obj); - obj_priv->last_rendering_seqno = i915_add_request(dev); + obj_priv->last_rendering_seqno = i915_add_request(dev, + write_domain); BUG_ON(obj_priv->last_rendering_seqno == 0); #if WATCH_LRU DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj); @@ -343,11 +409,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) if (ret != 0) return ret; - i915_gem_object_move_to_inactive(obj); - -#if WATCH_LRU - DRM_INFO("%s: wait moves to lru list %p\n", __func__, obj); -#endif + BUG_ON(obj_priv->active); } return 0; @@ -471,54 +533,73 @@ i915_gem_evict_something(struct drm_device *dev) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_gem_object *obj; struct drm_i915_gem_object *obj_priv; - int ret; - /* Find the LRU buffer. */ - if (!list_empty(&dev_priv->mm.inactive_list)) { - obj_priv = list_first_entry(&dev_priv->mm.inactive_list, - struct drm_i915_gem_object, - list); - } else if (!list_empty(&dev_priv->mm.active_list)) { - int found = 0; + for (;;) { + /* If there's an inactive buffer available now, grab it + * and be done. + */ + if (!list_empty(&dev_priv->mm.inactive_list)) { + obj_priv = list_first_entry(&dev_priv->mm.inactive_list, + struct drm_i915_gem_object, + list); + obj = obj_priv->obj; + BUG_ON(obj_priv->pin_count != 0); + break; + } - /* If there's nothing unused and ready, grab the first - * unpinned object from the currently executing list. + /* If we didn't get anything, but the ring is still processing + * things, wait for one of those things to finish and hopefully + * leave us a buffer to evict. */ - list_for_each_entry(obj_priv, &dev_priv->mm.active_list, - list) { - if (obj_priv->pin_count == 0) { - found = 1; - break; - } + if (!list_empty(&dev_priv->mm.request_list)) { + struct drm_i915_gem_request *request; + int ret; + + request = list_first_entry(&dev_priv->mm.request_list, + struct drm_i915_gem_request, + list); + + ret = i915_wait_request(dev, request->seqno); + if (ret != 0) + return ret; + + continue; } - if (!found) - return -ENOMEM; - } else { + + /* If we didn't have anything on the request list but there + * are buffers awaiting a flush, emit one and try again. + * When we wait on it, those buffers waiting for that flush + * will get moved to inactive. + */ + if (!list_empty(&dev_priv->mm.flushing_list)) { + obj_priv = list_first_entry(&dev_priv->mm.flushing_list, + struct drm_i915_gem_object, + list); + obj = obj_priv->obj; + + i915_gem_flush(dev, + obj->write_domain, + obj->write_domain); + i915_add_request(dev, obj->write_domain); + + obj = NULL; + continue; + } + + /* If we didn't do any of the above, there's nothing to be done + * and we just can't fit it in. + */ return -ENOMEM; } - obj = obj_priv->obj; - drm_gem_object_reference(obj); + #if WATCH_LRU DRM_INFO("%s: evicting %p\n", __func__, obj); #endif - /* Only unpinned buffers should be on this list. */ - BUG_ON(obj_priv->pin_count != 0); - - /* Do this separately from the wait_rendering in - * i915_gem_object_unbind() because we want to catch interrupts and - * return. - */ - ret = i915_gem_object_wait_rendering(obj); - if (ret != 0) - return ret; + BUG_ON(obj_priv->active); /* Wait on the rendering and unbind the buffer. */ i915_gem_object_unbind(obj); -#if WATCH_LRU - DRM_INFO("%s: evicted %p\n", __func__, obj); -#endif - drm_gem_object_unreference(obj); return 0; } @@ -713,12 +794,15 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, /** * Once all of the objects have been set in the proper domain, - * perform the necessary flush and invalidate operations + * perform the necessary flush and invalidate operations. + * + * Returns the write domains flushed, for use in flush tracking. */ - -static void +static uint32_t i915_gem_dev_set_domain(struct drm_device *dev) { + uint32_t flush_domains = dev->flush_domains; + /* * Now that all the buffers are synced to the proper domains, * flush and invalidate the collected domains @@ -736,6 +820,8 @@ i915_gem_dev_set_domain(struct drm_device *dev) dev->invalidate_domains = 0; dev->flush_domains = 0; } + + return flush_domains; } static int @@ -1014,7 +1100,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_gem_object *batch_obj; int ret, i; uint64_t exec_offset; - uint32_t seqno; + uint32_t seqno, flush_domains; LOCK_TEST_WITH_RETURN(dev, file_priv); @@ -1099,7 +1185,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } /* Flush/invalidate caches and chipset buffer */ - i915_gem_dev_set_domain(dev); + flush_domains = i915_gem_dev_set_domain(dev); exec_offset = validate_list[args->buffer_count - 1].offset; @@ -1124,7 +1210,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, * *some* interrupts representing completion of buffers that we can * wait on when trying to clear up gtt space). */ - seqno = i915_add_request(dev); + seqno = i915_add_request(dev, flush_domains); BUG_ON(seqno == 0); for (i = 0; i < args->buffer_count; i++) { struct drm_gem_object *obj = object_list[i]; -- cgit v1.2.3 From da3f099a7c4a18468ff84819ed39e42bec641e11 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 21 May 2008 00:32:02 -0700 Subject: [intel-gem] invalidate ring locals for pin/unpin/set_domain/free functions Ring locals must be reloaded from hardware in case the X server ran. --- linux-core/i915_gem.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index c67ce309..469b613f 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -408,8 +408,6 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) ret = i915_wait_request(dev, obj_priv->last_rendering_seqno); if (ret != 0) return ret; - - BUG_ON(obj_priv->active); } return 0; -- cgit v1.2.3 From 71b09a5f75c6063a592f7be07465761519839bcd Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 22 May 2008 10:59:59 -0700 Subject: [intel-gem] Force ring retire by emiting flush before user-interrupt. Commands in the ring are parsed and started when the head pointer passes by them, but they are not necessarily finished until a MI_FLUSH happens. This patch inserts a flush after the execbuffer (the only place a flush wasn't already happening). --- linux-core/i915_gem.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 469b613f..3214707f 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -161,6 +161,31 @@ i915_add_request(struct drm_device *dev, uint32_t flush_domains) return seqno; } +/** + * Command execution barrier + * + * Ensures that all commands in the ring are finished + * before signalling the CPU + */ + +uint32_t +i915_retire_commands(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + uint32_t cmd = CMD_MI_FLUSH | MI_NO_WRITE_FLUSH; + uint32_t flush_domains = 0; + RING_LOCALS; + + /* The sampler always gets flushed on i965 (sigh) */ + if (IS_I965G(dev)) + flush_domains |= DRM_GEM_DOMAIN_I915_SAMPLER; + BEGIN_LP_RING(2); + OUT_RING(cmd); + OUT_RING(0); /* noop */ + ADVANCE_LP_RING(); + return flush_domains; +} + /** * Moves buffers associated only with the given active seqno from the active * to inactive list, potentially freeing them. @@ -359,6 +384,9 @@ i915_gem_flush(struct drm_device *dev, if (invalidate_domains & DRM_GEM_DOMAIN_I915_INSTRUCTION) cmd |= MI_EXE_FLUSH; +#if WATCH_EXEC + DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd); +#endif BEGIN_LP_RING(2); OUT_RING(cmd); OUT_RING(0); /* noop */ @@ -1201,6 +1229,12 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } + /* + * Ensure that the commands in the batch buffer are + * finished before the interrupt fires + */ + flush_domains |= i915_retire_commands(dev); + /* * Get a seqno representing the execution of the current buffer, * which we can wait on. We would like to mitigate these interrupts, -- cgit v1.2.3 From 44ed693ca6f8d19acb39174c6efada070652a027 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 22 May 2008 11:34:56 -0700 Subject: [gem] Use CPU domain for new or pageable objects Newly allocated objects need to be in the CPU domain as they've just been cleared by the CPU. Also, unmapping objects from the GTT needs to put them into the CPU domain, both to flush rendering as well as to ensure that any paging action gets flushed before we remap to the GTT. --- linux-core/drm_gem.c | 8 ++++++++ linux-core/i915_gem.c | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index a8253bc5..fb175d7d 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -100,6 +100,14 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) kref_init(&obj->handlecount); obj->size = size; + /* + * We've just allocated pages from the kernel, + * so they've just been written by the CPU with + * zeros. They'll need to be clflushed before we + * use them with the GPU. + */ + obj->write_domain = DRM_GEM_DOMAIN_CPU; + obj->read_domains = DRM_GEM_DOMAIN_CPU; if (dev->driver->gem_init_object != NULL && dev->driver->gem_init_object(obj) != 0) { fput(obj->filp); diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 3214707f..60a8fa58 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -35,6 +35,11 @@ #define WATCH_LRU 0 #define WATCH_RELOC 0 +static void +i915_gem_object_set_domain(struct drm_gem_object *obj, + uint32_t read_domains, + uint32_t write_domain); + int i915_gem_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -456,11 +461,14 @@ i915_gem_object_unbind(struct drm_gem_object *obj) if (obj_priv->gtt_space == NULL) return; - /* Ignore the return value of wait_rendering. If we're here but - * a wait_rendering hasn't completed, we're in the freeing process, - * and we want the buffer to go away even if the command queue is hung. + /* Move the object to the CPU domain to ensure that + * any possible CPU writes while it's not in the GTT + * are flushed when we go to remap it. This will + * also ensure that all pending GPU writes are finished + * before we unbind. */ - (void)i915_gem_object_wait_rendering(obj); + i915_gem_object_set_domain (obj, DRM_GEM_DOMAIN_CPU, + DRM_GEM_DOMAIN_CPU); if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); -- cgit v1.2.3 From 8c2b207f9b1fb1cf6df23c7ef73ca57dfb5dd459 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 22 May 2008 23:08:38 -0700 Subject: [intel-gem] Encourage multiple caches to hold read data When reading from multiple domains, allow each cache to continue to hold data until writes occur somewhere. This is done by first leaving the read_domains alone at bind time (presumably the CPU read cache contains valid data still) and then in set_domain, if no write_domain is specified, the new read domains are simply merged into the existing read domains. A huge comment was added above set_domain to explain how things are expected to work. --- linux-core/i915_gem.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 118 insertions(+), 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 60a8fa58..b3b2dbce 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -746,12 +746,12 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) return -ENOMEM; } - /* When we have just bound an object, we have no valid read - * caches on it, regardless of where it was before. We also need - * an MI_FLUSH to occur so that the render and sampler TLBs - * get flushed and pick up our binding change above. + /* Assert that the object is not currently in any GPU domain. As it + * wasn't in the GTT, there shouldn't be any way it could have been in + * a GPU cache */ - obj->read_domains = 0; + BUG_ON(obj->read_domains & ~DRM_GEM_DOMAIN_CPU); + BUG_ON(obj->write_domain & ~DRM_GEM_DOMAIN_CPU); return 0; } @@ -775,6 +775,112 @@ i915_gem_clflush_object(struct drm_gem_object *obj) * Set the next domain for the specified object. This * may not actually perform the necessary flushing/invaliding though, * as that may want to be batched with other set_domain operations + * + * This is (we hope) the only really tricky part of gem. The goal + * is fairly simple -- track which caches hold bits of the object + * and make sure they remain coherent. A few concrete examples may + * help to explain how it works. For shorthand, we use the notation + * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the + * a pair of read and write domain masks. + * + * Case 1: the batch buffer + * + * 1. Allocated + * 2. Written by CPU + * 3. Mapped to GTT + * 4. Read by GPU + * 5. Unmapped from GTT + * 6. Freed + * + * Let's take these a step at a time + * + * 1. Allocated + * Pages allocated from the kernel may still have + * cache contents, so we set them to (CPU, CPU) always. + * 2. Written by CPU (using pwrite) + * The pwrite function calls set_domain (CPU, CPU) and + * this function does nothing (as nothing changes) + * 3. Mapped by GTT + * This function asserts that the object is not + * currently in any GPU-based read or write domains + * 4. Read by GPU + * i915_gem_execbuffer calls set_domain (COMMAND, 0). + * As write_domain is zero, this function adds in the + * current read domains (CPU+COMMAND, 0). + * flush_domains is set to CPU. + * invalidate_domains is set to COMMAND + * clflush is run to get data out of the CPU caches + * then i915_dev_set_domain calls i915_gem_flush to + * emit an MI_FLUSH and drm_agp_chipset_flush + * 5. Unmapped from GTT + * i915_gem_object_unbind calls set_domain (CPU, CPU) + * flush_domains and invalidate_domains end up both zero + * so no flushing/invalidating happens + * 6. Freed + * yay, done + * + * Case 2: The shared render buffer + * + * 1. Allocated + * 2. Mapped to GTT + * 3. Read/written by GPU + * 4. set_domain to (CPU,CPU) + * 5. Read/written by CPU + * 6. Read/written by GPU + * + * 1. Allocated + * Same as last example, (CPU, CPU) + * 2. Mapped to GTT + * Nothing changes (assertions find that it is not in the GPU) + * 3. Read/written by GPU + * execbuffer calls set_domain (RENDER, RENDER) + * flush_domains gets CPU + * invalidate_domains gets GPU + * clflush (obj) + * MI_FLUSH and drm_agp_chipset_flush + * 4. set_domain (CPU, CPU) + * flush_domains gets GPU + * invalidate_domains gets CPU + * wait_rendering (obj) to make sure all drawing is complete. + * This will include an MI_FLUSH to get the data from GPU + * to memory + * clflush (obj) to invalidate the CPU cache + * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?) + * 5. Read/written by CPU + * cache lines are loaded and dirtied + * 6. Read written by GPU + * Same as last GPU access + * + * Case 3: The constant buffer + * + * 1. Allocated + * 2. Written by CPU + * 3. Read by GPU + * 4. Updated (written) by CPU again + * 5. Read by GPU + * + * 1. Allocated + * (CPU, CPU) + * 2. Written by CPU + * (CPU, CPU) + * 3. Read by GPU + * (CPU+RENDER, 0) + * flush_domains = CPU + * invalidate_domains = RENDER + * clflush (obj) + * MI_FLUSH + * drm_agp_chipset_flush + * 4. Updated (written) by CPU again + * (CPU, CPU) + * flush_domains = 0 (no previous write domain) + * invalidate_domains = 0 (no new read domains) + * 5. Read by GPU + * (CPU+RENDER, 0) + * flush_domains = CPU + * invalidate_domains = RENDER + * clflush (obj) + * MI_FLUSH + * drm_agp_chipset_flush */ static void i915_gem_object_set_domain(struct drm_gem_object *obj, @@ -789,6 +895,13 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, DRM_INFO("%s: object %p read %08x write %08x\n", __func__, obj, read_domains, write_domain); #endif + /* + * If the object isn't moving to a new write domain, + * let the object stay in multiple read domains + */ + if (write_domain == 0) + read_domains |= obj->read_domains; + /* * Flush the current write domain if * the new read domains don't match. Invalidate -- cgit v1.2.3 From c69b81df62cb7e04f956f2cf77091216754c3632 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 25 May 2008 20:41:42 -0700 Subject: [intel-gem] replace call to jiffies_to-msec with simple inline --- linux-core/i915_gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index b3b2dbce..c53a39ec 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1221,7 +1221,7 @@ i915_gem_ring_throttle(struct drm_device *dev) list); /* Break out if we're close enough. */ - if (jiffies_to_msecs(jiffies - request->emitted_jiffies) < 20) { + if ((long) (jiffies - request->emitted_jiffies) <= (20 * HZ) / 1000) { mutex_unlock(&dev->struct_mutex); return 0; } -- cgit v1.2.3 From 6d1d11704ab36e4ee50b2c1d3b984ab6bb691417 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 25 May 2008 20:44:19 -0700 Subject: [intel-gem] Compute npages instead of nbytes in flush_pwrite i915_gem_flush_pwrite optimizes short writes to the buffer by clflushing only the modified pages, but it was miscomputing the number of pages. --- linux-core/i915_gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index c53a39ec..b5c87747 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1523,7 +1523,7 @@ i915_gem_flush_pwrite(struct drm_gem_object *obj, if ((size < obj->size >> 1) && obj_priv->page_list != NULL) { unsigned long first_page = offset / PAGE_SIZE; - unsigned long beyond_page = roundup(offset + size, PAGE_SIZE); + unsigned long beyond_page = roundup(offset + size, PAGE_SIZE) / PAGE_SIZE; drm_ttm_cache_flush(obj_priv->page_list + first_page, beyond_page - first_page); -- cgit v1.2.3 From 7cf3fd29fe058a0bfc2ba7e889d1b360398be161 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 25 May 2008 20:45:20 -0700 Subject: [intel-gem] Add DRM_I915_GEM_BUSY ioctl to check for idle buffers. This new ioctl returns whether re-using the buffer would force a wait. --- linux-core/i915_gem.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index b5c87747..99dc00fc 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1471,6 +1471,31 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, return 0; } +int +i915_gem_busy_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_busy *args = data; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + + mutex_lock(&dev->struct_mutex); + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) { + DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n", + args->handle); + mutex_unlock(&dev->struct_mutex); + return -EINVAL; + } + + obj_priv = obj->driver_private; + args->busy = obj_priv->active; + + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + return 0; +} + int i915_gem_init_object(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv; -- cgit v1.2.3 From d434b64f6a760d85295e32298a9a1f3624ee1b69 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 26 May 2008 03:25:16 -0700 Subject: [i915] leave interrupts masked off when not in use. The interrupt enable register cannot be used to temporarily disable interrupts, instead use the interrupt mask register. Note that this change means that a pile of buffers will be left stuck on the chip as the final interrupts will not be recognized to come and drain things. --- linux-core/i915_gem.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 99dc00fc..3ad3f40c 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -311,11 +311,13 @@ i915_wait_request(struct drm_device *dev, uint32_t seqno) BUG_ON(seqno == 0); - i915_user_irq_on(dev_priv); - ret = wait_event_interruptible(dev_priv->irq_queue, - i915_seqno_passed(i915_get_gem_seqno(dev), - seqno)); - i915_user_irq_off(dev_priv); + if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) { + i915_user_irq_on(dev_priv); + ret = wait_event_interruptible(dev_priv->irq_queue, + i915_seqno_passed(i915_get_gem_seqno(dev), + seqno)); + i915_user_irq_off(dev_priv); + } /* Directly dispatch request retiring. While we have the work queue * to handle this, the waiter on a request often wants an associated @@ -1538,6 +1540,7 @@ int i915_gem_flush_pwrite(struct drm_gem_object *obj, uint64_t offset, uint64_t size) { +#if 0 struct drm_device *dev = obj->dev; struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -1555,6 +1558,7 @@ i915_gem_flush_pwrite(struct drm_gem_object *obj, drm_agp_chipset_flush(dev); obj->write_domain = 0; } +#endif return 0; } -- cgit v1.2.3 From 1f4e36081bd6ff7d7b53a62e0c8db7c0f82edf99 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 26 May 2008 17:41:46 -0700 Subject: [intel-gem] Must hold DRM lock while setting object domain Object domain transfer can involve adding flush ops to the request queue, and so the DRM lock must be held to avoid having the X server smash pointers badly. --- linux-core/i915_gem.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 3ad3f40c..5eeabdaa 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1529,9 +1529,11 @@ i915_gem_set_domain(struct drm_gem_object *obj, BUG_ON(!mutex_is_locked(&dev->struct_mutex)); + drm_idlelock_take (&dev->lock); i915_kernel_lost_context(dev); i915_gem_object_set_domain(obj, read_domains, write_domain); i915_gem_dev_set_domain(obj->dev); + drm_idlelock_release (&dev->lock); return 0; } -- cgit v1.2.3 From e10502002f0ebb2b56b19384b2f2eae7a7a84512 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 27 May 2008 17:50:39 -0700 Subject: [intel-gem] Replace idlelock usage with real lock acquisition. --- linux-core/drmP.h | 9 +++++++ linux-core/drm_gem.c | 6 ++--- linux-core/drm_lock.c | 74 ++++++++++++++++++++++++++++++++++++++++++--------- linux-core/i915_gem.c | 5 ++-- 4 files changed, 76 insertions(+), 18 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index fc7043d7..8246f44a 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -474,6 +474,11 @@ struct drm_lock_data { uint32_t kernel_waiters; uint32_t user_waiters; int idle_has_lock; + /** + * Boolean signaling that the lock is held on behalf of the + * file_priv client by the kernel in an ioctl handler. + */ + int kernel_held; }; /** @@ -778,6 +783,7 @@ struct drm_driver { * Driver-specific callback to set memory domains from userspace */ int (*gem_set_domain) (struct drm_gem_object *obj, + struct drm_file *file_priv, uint32_t read_domains, uint32_t write_domain); @@ -1178,6 +1184,9 @@ extern int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context); extern int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context); extern void drm_idlelock_take(struct drm_lock_data *lock_data); extern void drm_idlelock_release(struct drm_lock_data *lock_data); +extern int drm_client_lock_take(struct drm_device *dev, + struct drm_file *file_priv); +extern void drm_client_lock_release(struct drm_device *dev); /* * These are exported to drivers so that they can implement fencing using diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index fb175d7d..b726e598 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -291,7 +291,7 @@ drm_gem_pread_ioctl(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain(obj, + ret = dev->driver->gem_set_domain(obj, file_priv, DRM_GEM_DOMAIN_CPU, 0); if (ret) { @@ -384,7 +384,7 @@ drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain(obj, + ret = dev->driver->gem_set_domain(obj, file_priv, DRM_GEM_DOMAIN_CPU, DRM_GEM_DOMAIN_CPU); if (ret) { @@ -530,7 +530,7 @@ drm_gem_set_domain_ioctl(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain(obj, + ret = dev->driver->gem_set_domain(obj, file_priv, args->read_domains, args->write_domain); } else { diff --git a/linux-core/drm_lock.c b/linux-core/drm_lock.c index db1d646b..ad7c1679 100644 --- a/linux-core/drm_lock.c +++ b/linux-core/drm_lock.c @@ -215,22 +215,16 @@ int drm_lock_take(struct drm_lock_data *lock_data, } while (prev != old); spin_unlock_irqrestore(&lock_data->spinlock, irqflags); - if (_DRM_LOCKING_CONTEXT(old) == context) { - if (old & _DRM_LOCK_HELD) { - if (context != DRM_KERNEL_CONTEXT) { - DRM_ERROR("%d holds heavyweight lock\n", - context); - } - return 0; + /* Warn on recursive locking of user contexts. */ + if (_DRM_LOCKING_CONTEXT(old) == context && _DRM_LOCK_IS_HELD(old)) { + if (context != DRM_KERNEL_CONTEXT) { + DRM_ERROR("%d holds heavyweight lock\n", + context); } + return 0; } - if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) { - /* Have lock */ - - return 1; - } - return 0; + return !_DRM_LOCK_IS_HELD(old); } /** @@ -386,6 +380,60 @@ void drm_idlelock_release(struct drm_lock_data *lock_data) } EXPORT_SYMBOL(drm_idlelock_release); +/** + * Takes the lock on behalf of the client if needed, using the kernel context. + * + * This allows us to hide the hardware lock when it's required for protection + * of data structures (such as command ringbuffer) shared with the X Server, and + * a way for us to transition to lockless for those requests when the X Server + * stops accessing the ringbuffer directly, without having to update the + * other userland clients. + */ +int drm_client_lock_take(struct drm_device *dev, struct drm_file *file_priv) +{ + int ret; + unsigned long irqflags; + + /* If the client has the lock, we're already done. */ + if (drm_i_have_hw_lock(dev, file_priv)) + return 0; + + /* Client doesn't hold the lock. Block taking the lock with the kernel + * context on behalf of the client, and return whether we were + * successful. + */ + spin_lock_irqsave(&dev->lock.spinlock, irqflags); + dev->lock.user_waiters++; + spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); + ret = wait_event_interruptible(dev->lock.lock_queue, + drm_lock_take(&dev->lock, + DRM_KERNEL_CONTEXT)); + spin_lock_irqsave(&dev->lock.spinlock, irqflags); + dev->lock.user_waiters--; + if (ret != 0) { + spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); + return ret; + } else { + dev->lock.file_priv = file_priv; + dev->lock.lock_time = jiffies; + dev->lock.kernel_held = 1; + file_priv->lock_count++; + spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); + return 0; + } +} +EXPORT_SYMBOL(drm_client_lock_take); + +void drm_client_lock_release(struct drm_device *dev) +{ + if (dev->lock.kernel_held) { + dev->lock.kernel_held = 0; + dev->lock.file_priv = NULL; + drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT); + } +} +EXPORT_SYMBOL(drm_client_lock_release); + int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv) { diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 5eeabdaa..ebe51539 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1522,6 +1522,7 @@ void i915_gem_free_object(struct drm_gem_object *obj) int i915_gem_set_domain(struct drm_gem_object *obj, + struct drm_file *file_priv, uint32_t read_domains, uint32_t write_domain) { @@ -1529,11 +1530,11 @@ i915_gem_set_domain(struct drm_gem_object *obj, BUG_ON(!mutex_is_locked(&dev->struct_mutex)); - drm_idlelock_take (&dev->lock); + drm_client_lock_take(dev, file_priv); i915_kernel_lost_context(dev); i915_gem_object_set_domain(obj, read_domains, write_domain); i915_gem_dev_set_domain(obj->dev); - drm_idlelock_release (&dev->lock); + drm_client_lock_release(dev); return 0; } -- cgit v1.2.3 From 19ff3366e4ed591741af4bcf49991823115bdb17 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 27 May 2008 16:49:49 -0700 Subject: [intel-gem] Clean up active/inactive/flushing list debugging. --- linux-core/i915_gem.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index ebe51539..1fc48e0c 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -240,7 +240,7 @@ i915_gem_retire_request(struct drm_device *dev, return; #if WATCH_LRU DRM_INFO("%s: retire %d moves to inactive list %p\n", - __func__, seqno, obj); + __func__, request->seqno, obj); #endif if (obj->write_domain != 0) { @@ -547,14 +547,23 @@ i915_dump_lru(struct drm_device *dev, const char *where) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv; - DRM_INFO("GTT execution list %s {\n", where); + DRM_INFO("active list %s {\n", where); list_for_each_entry(obj_priv, &dev_priv->mm.active_list, list) { DRM_INFO(" %p: %08x\n", obj_priv, obj_priv->last_rendering_seqno); } - DRM_INFO("GTT LRU %s {\n", where); + DRM_INFO("}\n"); + DRM_INFO("flushing list %s {\n", where); + list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, + list) + { + DRM_INFO(" %p: %08x\n", obj_priv, + obj_priv->last_rendering_seqno); + } + DRM_INFO("}\n"); + DRM_INFO("inactive %s {\n", where); list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { DRM_INFO(" %p: %08x\n", obj_priv, obj_priv->last_rendering_seqno); @@ -1377,7 +1386,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, DRM_INFO("%s: move to exec list %p\n", __func__, obj); #endif } -#if WATCH_LRU && 0 +#if WATCH_LRU i915_dump_lru(dev, __func__); #endif -- cgit v1.2.3 From 3b1e4e6dc38029e697afb8e6ec81ebbed7adf442 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 29 May 2008 12:53:13 -0700 Subject: [intel-gem] Write the presumed_offset back out after updating it. Otherwise, 965 constant state buffers get re-relocated every exec. Ouch. --- linux-core/i915_gem.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 1fc48e0c..52a57d5e 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1137,6 +1137,16 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, #endif writel(reloc_val, reloc_entry); + /* Write the updated presumed offset for this entry back out + * to the user. + */ + reloc.presumed_offset = target_obj_priv->gtt_offset; + ret = copy_to_user(relocs + i, &reloc, sizeof(reloc)); + if (ret != 0) { + drm_gem_object_unreference(target_obj); + return ret; + } + drm_gem_object_unreference(target_obj); } -- cgit v1.2.3 From 4f92ed34270ae4afaa0ddba38d227c6e359bcc98 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 30 May 2008 10:04:22 -0700 Subject: [intel-gem] Add an option to check GTT versus CPU coherency at execbuf time. --- linux-core/i915_gem.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 52a57d5e..7c826aeb 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -30,6 +30,7 @@ #include "i915_drm.h" #include "i915_drv.h" +#define WATCH_COHERENCY 0 #define WATCH_BUF 0 #define WATCH_EXEC 0 #define WATCH_LRU 0 @@ -982,6 +983,76 @@ i915_gem_dev_set_domain(struct drm_device *dev) return flush_domains; } +#if WATCH_COHERENCY +static void +i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle) +{ + struct drm_device *dev = obj->dev; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + int page; + uint32_t *gtt_mapping; + uint32_t *backing_map = NULL; + int bad_count = 0; + + DRM_INFO("%s: checking coherency of object %p@0x%08x (%d, %dkb):\n", + __FUNCTION__, obj, obj_priv->gtt_offset, handle, + obj->size / 1024); + + gtt_mapping = ioremap(dev->agp->base + obj_priv->gtt_offset, + obj->size); + if (gtt_mapping == NULL) { + DRM_ERROR("failed to map GTT space\n"); + return; + } + + for (page = 0; page < obj->size / PAGE_SIZE; page++) { + int i; + + backing_map = kmap_atomic(obj_priv->page_list[page], KM_USER0); + + if (backing_map == NULL) { + DRM_ERROR("failed to map backing page\n"); + goto out; + } + + for (i = 0; i < PAGE_SIZE / 4; i++) { + uint32_t cpuval = backing_map[i]; + uint32_t gttval = readl(gtt_mapping + + page * 1024 + i); + + if (cpuval != gttval) { + DRM_INFO("incoherent CPU vs GPU at 0x%08x: " + "0x%08x vs 0x%08x\n", + (int)(obj_priv->gtt_offset + + page * PAGE_SIZE + i * 4), + cpuval, gttval); + if (bad_count++ >= 8) { + DRM_INFO("...\n"); + goto out; + } + } + } + kunmap_atomic(backing_map, KM_USER0); + backing_map = NULL; + } + + out: + if (backing_map != NULL) + kunmap_atomic(backing_map, KM_USER0); + iounmap(gtt_mapping); + + /* give syslog time to catch up */ + msleep(1); + + /* Directly flush the object, since we just loaded values with the CPU + * from thebacking pages and we don't want to disturb the cache + * management that we're trying to observe. + */ + + i915_gem_clflush_object(obj); +} +#endif + static int i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, struct drm_file *file_priv, @@ -1355,6 +1426,13 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* Flush/invalidate caches and chipset buffer */ flush_domains = i915_gem_dev_set_domain(dev); +#if WATCH_COHERENCY + for (i = 0; i < args->buffer_count; i++) { + i915_gem_object_check_coherency(object_list[i], + validate_list[i].handle); + } +#endif + exec_offset = validate_list[args->buffer_count - 1].offset; #if WATCH_EXEC -- cgit v1.2.3 From 50bce2bc625deb439dd61f504496dddd0cd4f572 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 30 May 2008 13:47:34 -0700 Subject: [intel-gem] Only update obj->write_domain if we're actually changing it. The problem was revealed where on 965, the display list vertex buffer would see: create -> (CPU, CPU) set_domain (CPU, CPU) -> (CPU, CPU) set_comain (CPU, 0) -> (CPU, 0) (no clflush occurred) execbuf (GPU, 0) -> (CPU+GPU, 0) (still no clflush) instead of: create -> (CPU, CPU) set_domain (CPU, CPU) -> (CPU, CPU) set_comain (CPU, 0) -> (CPU, CPU) execbuf (GPU, 0) -> (CPU+GPU, 0) (clflushed) --- linux-core/i915_gem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 7c826aeb..bcc15dd3 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -945,7 +945,8 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, i915_gem_clflush_object(obj); } - obj->write_domain = write_domain; + if ((write_domain | flush_domains) != 0) + obj->write_domain = write_domain; obj->read_domains = read_domains; dev->invalidate_domains |= invalidate_domains; dev->flush_domains |= flush_domains; @@ -1225,7 +1226,8 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, iounmap(reloc_page); #if WATCH_BUF - i915_gem_dump_object(obj, 128, __func__, ~0); + if (0) + i915_gem_dump_object(obj, 128, __func__, ~0); #endif return 0; } -- cgit v1.2.3 From 1cb2940a252f970bad0f88a5f14b4d39ea53ef1f Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 2 Jun 2008 10:59:15 -0700 Subject: [intel-gem] Propagate set_domain errors. set_domain can block waiting for rendering to complete. If that process is interrupted by a signal, it can return -EINTR. Catch this error in all callers and correctly deal with the result. --- linux-core/i915_gem.c | 58 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 17 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index bcc15dd3..4e01b8ea 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -36,7 +36,7 @@ #define WATCH_LRU 0 #define WATCH_RELOC 0 -static void +static int i915_gem_object_set_domain(struct drm_gem_object *obj, uint32_t read_domains, uint32_t write_domain); @@ -452,17 +452,18 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) /** * Unbinds an object from the GTT aperture. */ -static void +static int i915_gem_object_unbind(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; + int ret = 0; #if WATCH_BUF DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj); DRM_INFO("gtt_space %p\n", obj_priv->gtt_space); #endif if (obj_priv->gtt_space == NULL) - return; + return 0; /* Move the object to the CPU domain to ensure that * any possible CPU writes while it's not in the GTT @@ -470,8 +471,10 @@ i915_gem_object_unbind(struct drm_gem_object *obj) * also ensure that all pending GPU writes are finished * before we unbind. */ - i915_gem_object_set_domain (obj, DRM_GEM_DOMAIN_CPU, - DRM_GEM_DOMAIN_CPU); + ret = i915_gem_object_set_domain (obj, DRM_GEM_DOMAIN_CPU, + DRM_GEM_DOMAIN_CPU); + if (ret) + return ret; if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); @@ -494,6 +497,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) drm_gem_object_unreference(obj); } } + return 0; } #if WATCH_BUF | WATCH_EXEC @@ -579,6 +583,7 @@ i915_gem_evict_something(struct drm_device *dev) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_gem_object *obj; struct drm_i915_gem_object *obj_priv; + int ret; for (;;) { /* If there's an inactive buffer available now, grab it @@ -645,9 +650,9 @@ i915_gem_evict_something(struct drm_device *dev) BUG_ON(obj_priv->active); /* Wait on the rendering and unbind the buffer. */ - i915_gem_object_unbind(obj); + ret = i915_gem_object_unbind(obj); - return 0; + return ret; } static int @@ -894,7 +899,7 @@ i915_gem_clflush_object(struct drm_gem_object *obj) * MI_FLUSH * drm_agp_chipset_flush */ -static void +static int i915_gem_object_set_domain(struct drm_gem_object *obj, uint32_t read_domains, uint32_t write_domain) @@ -902,6 +907,7 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, struct drm_device *dev = obj->dev; uint32_t invalidate_domains = 0; uint32_t flush_domains = 0; + int ret; #if WATCH_BUF DRM_INFO("%s: object %p read %08x write %08x\n", @@ -940,8 +946,11 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, * flushed before the cpu cache is invalidated */ if ((invalidate_domains & DRM_GEM_DOMAIN_CPU) && - (flush_domains & ~DRM_GEM_DOMAIN_CPU)) - i915_gem_object_wait_rendering(obj); + (flush_domains & ~DRM_GEM_DOMAIN_CPU)) { + ret = i915_gem_object_wait_rendering(obj); + if (ret) + return ret; + } i915_gem_clflush_object(obj); } @@ -950,6 +959,7 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, obj->read_domains = read_domains; dev->invalidate_domains |= invalidate_domains; dev->flush_domains |= flush_domains; + return 0; } /** @@ -1378,6 +1388,14 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } mutex_lock(&dev->struct_mutex); + + /* Zero the gloabl flush/invalidate flags. These + * will be modified as each object is bound to the + * gtt + */ + dev->invalidate_domains = 0; + dev->flush_domains = 0; + /* Look up object handles and perform the relocations */ for (i = 0; i < args->buffer_count; i++) { object_list[i] = drm_gem_object_lookup(dev, file_priv, @@ -1389,6 +1407,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } + object_list[i]->pending_read_domains = 0; + object_list[i]->pending_write_domain = 0; ret = i915_gem_reloc_and_validate_object(object_list[i], file_priv, &validate_list[i]); @@ -1418,11 +1438,11 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, } /* make sure all previous memory operations have passed */ - i915_gem_object_set_domain(obj, - obj->pending_read_domains, - obj->pending_write_domain); - obj->pending_read_domains = 0; - obj->pending_write_domain = 0; + ret = i915_gem_object_set_domain(obj, + obj->pending_read_domains, + obj->pending_write_domain); + if (ret) + goto err; } /* Flush/invalidate caches and chipset buffer */ @@ -1626,15 +1646,19 @@ i915_gem_set_domain(struct drm_gem_object *obj, uint32_t write_domain) { struct drm_device *dev = obj->dev; + int ret; BUG_ON(!mutex_is_locked(&dev->struct_mutex)); drm_client_lock_take(dev, file_priv); i915_kernel_lost_context(dev); - i915_gem_object_set_domain(obj, read_domains, write_domain); + ret = i915_gem_object_set_domain(obj, read_domains, write_domain); + if (ret) { + drm_client_lock_release(dev); + return ret; + } i915_gem_dev_set_domain(obj->dev); drm_client_lock_release(dev); - return 0; } -- cgit v1.2.3 From 867c2bb461e4bf7765fdbf502f625b739ceecb96 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 2 Jun 2008 12:37:10 -0700 Subject: =?UTF-8?q?[intel-gem]=20reloc=5Fand=5Fvalidate=5Fobject=20?= =?UTF-8?q?=E2=86=92=20object=5Fbind=5Fand=5Frelocate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just renaming this function and related parameters to match terminology used elsewhere in the driver. --- linux-core/i915_gem.c | 64 +++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 28 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 4e01b8ea..ad73f0a0 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1064,10 +1064,15 @@ i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle) } #endif +/** + * Bind an object to the GTT and evaluate the relocations landing in it + * + * + */ static int -i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, - struct drm_file *file_priv, - struct drm_i915_gem_exec_object *entry) +i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, + struct drm_file *file_priv, + struct drm_i915_gem_exec_object *entry) { struct drm_device *dev = obj->dev; struct drm_i915_gem_relocation_entry reloc; @@ -1108,7 +1113,7 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, target_obj_priv = target_obj->driver_private; /* The target buffer should have appeared before us in the - * validate list, so it should have a GTT space bound by now. + * exec_object list, so it should have a GTT space bound by now. */ if (target_obj_priv->gtt_space == NULL) { DRM_ERROR("No GTT space found for object %d\n", @@ -1242,6 +1247,8 @@ i915_gem_reloc_and_validate_object(struct drm_gem_object *obj, return 0; } +/** Dispatch a batchbuffer to the ring + */ static int i915_dispatch_gem_execbuffer(struct drm_device *dev, struct drm_i915_gem_execbuffer *exec, @@ -1346,7 +1353,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) { struct drm_i915_gem_execbuffer *args = data; - struct drm_i915_gem_exec_object *validate_list = NULL; + struct drm_i915_gem_exec_object *exec_list = NULL; struct drm_gem_object **object_list = NULL; struct drm_gem_object *batch_obj; int ret, i; @@ -1365,26 +1372,26 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, if (ret) return ret; - /* Copy in the validate list from userland */ - validate_list = drm_calloc(sizeof(*validate_list), args->buffer_count, - DRM_MEM_DRIVER); + /* Copy in the exec list from userland */ + exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count, + DRM_MEM_DRIVER); object_list = drm_calloc(sizeof(*object_list), args->buffer_count, DRM_MEM_DRIVER); - if (validate_list == NULL || object_list == NULL) { - DRM_ERROR("Failed to allocate validate or object list " + if (exec_list == NULL || object_list == NULL) { + DRM_ERROR("Failed to allocate exec or object list " "for %d buffers\n", args->buffer_count); ret = -ENOMEM; - goto err; + goto pre_mutex_err; } - ret = copy_from_user(validate_list, + ret = copy_from_user(exec_list, (struct drm_i915_relocation_entry __user *) (uintptr_t) args->buffers_ptr, - sizeof(*validate_list) * args->buffer_count); + sizeof(*exec_list) * args->buffer_count); if (ret != 0) { - DRM_ERROR("copy %d validate entries failed %d\n", + DRM_ERROR("copy %d exec entries failed %d\n", args->buffer_count, ret); - goto err; + goto pre_mutex_err; } mutex_lock(&dev->struct_mutex); @@ -1399,21 +1406,21 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* Look up object handles and perform the relocations */ for (i = 0; i < args->buffer_count; i++) { object_list[i] = drm_gem_object_lookup(dev, file_priv, - validate_list[i].handle); + exec_list[i].handle); if (object_list[i] == NULL) { DRM_ERROR("Invalid object handle %d at index %d\n", - validate_list[i].handle, i); + exec_list[i].handle, i); ret = -EINVAL; goto err; } object_list[i]->pending_read_domains = 0; object_list[i]->pending_write_domain = 0; - ret = i915_gem_reloc_and_validate_object(object_list[i], - file_priv, - &validate_list[i]); + ret = i915_gem_object_bind_and_relocate(object_list[i], + file_priv, + &exec_list[i]); if (ret) { - DRM_ERROR("reloc and validate failed %d\n", ret); + DRM_ERROR("object bind and relocate failed %d\n", ret); goto err; } } @@ -1451,11 +1458,11 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, #if WATCH_COHERENCY for (i = 0; i < args->buffer_count; i++) { i915_gem_object_check_coherency(object_list[i], - validate_list[i].handle); + exec_list[i].handle); } #endif - exec_offset = validate_list[args->buffer_count - 1].offset; + exec_offset = exec_list[args->buffer_count - 1].offset; #if WATCH_EXEC i915_gem_dump_object(object_list[args->buffer_count - 1], @@ -1500,13 +1507,13 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, i915_dump_lru(dev, __func__); #endif - /* Copy the new buffer offsets back to the user's validate list. */ + /* Copy the new buffer offsets back to the user's exec list. */ ret = copy_to_user((struct drm_i915_relocation_entry __user *) (uintptr_t) args->buffers_ptr, - validate_list, - sizeof(*validate_list) * args->buffer_count); + exec_list, + sizeof(*exec_list) * args->buffer_count); if (ret) - DRM_ERROR("failed to copy %d validate entries " + DRM_ERROR("failed to copy %d exec entries " "back to user (%d)\n", args->buffer_count, ret); err: @@ -1516,9 +1523,10 @@ err: } mutex_unlock(&dev->struct_mutex); +pre_mutex_err: drm_free(object_list, sizeof(*object_list) * args->buffer_count, DRM_MEM_DRIVER); - drm_free(validate_list, sizeof(*validate_list) * args->buffer_count, + drm_free(exec_list, sizeof(*exec_list) * args->buffer_count, DRM_MEM_DRIVER); return ret; -- cgit v1.2.3 From 0903de0c8f7d2566c1bd65600142a71572eec07e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 3 Jun 2008 21:49:51 -0700 Subject: Drop struct_mutex while waiting in drm_client_lock_take struct_mutex cannot be held while blocking on DRM lock. --- linux-core/drm_lock.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_lock.c b/linux-core/drm_lock.c index 58c5f08d..e6eae42b 100644 --- a/linux-core/drm_lock.c +++ b/linux-core/drm_lock.c @@ -393,6 +393,7 @@ int drm_client_lock_take(struct drm_device *dev, struct drm_file *file_priv) if (drm_i_have_hw_lock(dev, file_priv)) return 0; + mutex_unlock (&dev->struct_mutex); /* Client doesn't hold the lock. Block taking the lock with the kernel * context on behalf of the client, and return whether we were * successful. @@ -407,15 +408,15 @@ int drm_client_lock_take(struct drm_device *dev, struct drm_file *file_priv) dev->lock.user_waiters--; if (ret != 0) { spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); - return ret; } else { dev->lock.file_priv = file_priv; dev->lock.lock_time = jiffies; dev->lock.kernel_held = 1; file_priv->lock_count++; spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); - return 0; } + mutex_lock (&dev->struct_mutex); + return ret; } EXPORT_SYMBOL(drm_client_lock_take); -- cgit v1.2.3 From 118baeee1820102177f4f5bb48dd2a1e3d95d21e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 5 Jun 2008 13:47:41 -0700 Subject: [intel-gem] Dump error status on wait_request failure --- linux-core/i915_gem.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index ad73f0a0..268411e8 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -319,6 +319,9 @@ i915_wait_request(struct drm_device *dev, uint32_t seqno) seqno)); i915_user_irq_off(dev_priv); } + if (ret) + DRM_ERROR ("%s returns %d (awaiting %d at %d)\n", + __func__, ret, seqno, i915_get_gem_seqno(dev)); /* Directly dispatch request retiring. While we have the work queue * to handle this, the waiter on a request often wants an associated -- cgit v1.2.3 From 56a96841d01d112d7d4adfebb572016398551ba8 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 6 Jun 2008 12:57:01 -0700 Subject: [intel-gem] Add explicit throttle ioctl Instead of throttling and execbuffer time, have the application ask to throttle explicitly. This allows the throttle to happen less often, and without holding the DRM lock. --- linux-core/i915_gem.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 268411e8..14e57b41 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1371,10 +1371,6 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, #endif i915_kernel_lost_context(dev); - ret = i915_gem_ring_throttle(dev); - if (ret) - return ret; - /* Copy in the exec list from userland */ exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count, DRM_MEM_DRIVER); @@ -1628,6 +1624,13 @@ i915_gem_busy_ioctl(struct drm_device *dev, void *data, return 0; } +int +i915_gem_throttle_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + return i915_gem_ring_throttle(dev); +} + int i915_gem_init_object(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv; -- cgit v1.2.3 From 9f46c6935d154743162c6239903a4a9e443907bc Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 6 Jun 2008 12:59:52 -0700 Subject: [intel-gem] Use timers to retire requests periodically. Without the user IRQ running constantly, there's no wakeup when the ring empties to go retire requests and free buffers. Use a 1 second timer to make that happen more often. --- linux-core/drm_irq.c | 1 + linux-core/i915_gem.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) (limited to 'linux-core') diff --git a/linux-core/drm_irq.c b/linux-core/drm_irq.c index 8f27d7f3..318d9d7a 100644 --- a/linux-core/drm_irq.c +++ b/linux-core/drm_irq.c @@ -124,6 +124,7 @@ int drm_vblank_init(struct drm_device *dev, int num_crtcs) setup_timer(&dev->vblank_disable_timer, vblank_disable_fn, (unsigned long)dev); + init_timer_deferrable(&dev->vblank_disable_timer); spin_lock_init(&dev->vbl_lock); atomic_set(&dev->vbl_signal_pending, 0); dev->num_crtcs = num_crtcs; diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 14e57b41..abc929e9 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -162,6 +162,9 @@ i915_add_request(struct drm_device *dev, uint32_t flush_domains) request->seqno = seqno; request->emitted_jiffies = jiffies; request->flush_domains = flush_domains; + if (list_empty(&dev_priv->mm.request_list)) + mod_timer(&dev_priv->mm.retire_timer, jiffies + HZ); + list_add_tail(&request->list, &dev_priv->mm.request_list); return seqno; @@ -300,6 +303,32 @@ i915_gem_retire_requests(struct drm_device *dev) } } +void +i915_gem_retire_timeout(unsigned long data) +{ + struct drm_device *dev = (struct drm_device *) data; + drm_i915_private_t *dev_priv = dev->dev_private; + + schedule_work(&dev_priv->mm.retire_task); +} + +void +i915_gem_retire_handler(struct work_struct *work) +{ + drm_i915_private_t *dev_priv; + struct drm_device *dev; + + dev_priv = container_of(work, drm_i915_private_t, + mm.retire_task); + dev = dev_priv->dev; + + mutex_lock(&dev->struct_mutex); + i915_gem_retire_requests(dev); + if (!list_empty(&dev_priv->mm.request_list)) + mod_timer(&dev_priv->mm.retire_timer, jiffies + HZ); + mutex_unlock(&dev->struct_mutex); +} + /** * Waits for a sequence number to be signaled, and cleans up the * request and object lists appropriately for that event. -- cgit v1.2.3 From 6cd0ef06a6c2bdcede166d9a2d0434e58e4a01f2 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 6 Jun 2008 13:26:03 -0700 Subject: [intel] remove settable use_mi_batchbuffer_start The driver can know what hardware requires MI_BATCH_BUFFER vs MI_BATCH_BUFFER_START; there's no reason to let user mode configure this. --- linux-core/i915_gem.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index abc929e9..d60a98f8 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1315,7 +1315,14 @@ i915_dispatch_gem_execbuffer(struct drm_device *dev, return ret; } - if (dev_priv->use_mi_batchbuffer_start) { + if (IS_I830(dev) || IS_845G(dev)) { + BEGIN_LP_RING(4); + OUT_RING(MI_BATCH_BUFFER); + OUT_RING(exec_start | MI_BATCH_NON_SECURE); + OUT_RING(exec_start + exec_len - 4); + OUT_RING(0); + ADVANCE_LP_RING(); + } else { BEGIN_LP_RING(2); if (IS_I965G(dev)) { OUT_RING(MI_BATCH_BUFFER_START | @@ -1328,14 +1335,6 @@ i915_dispatch_gem_execbuffer(struct drm_device *dev, OUT_RING(exec_start | MI_BATCH_NON_SECURE); } ADVANCE_LP_RING(); - - } else { - BEGIN_LP_RING(4); - OUT_RING(MI_BATCH_BUFFER); - OUT_RING(exec_start | MI_BATCH_NON_SECURE); - OUT_RING(exec_start + exec_len - 4); - OUT_RING(0); - ADVANCE_LP_RING(); } } -- cgit v1.2.3 From 2150da5d1a57d25d0f4bc39bb6c883d410f586d1 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 10 Jun 2008 15:30:23 -0700 Subject: [gem] Manage the ringbuffer from the kernel in the GEM case. This requires that the X Server use the execbuf interface for buffer submission, as it no longer has direct access to the ring. This is therefore a flag day for the gem interface. This also adds enter/leavevt ioctls for use by the X Server. These would get stubbed out in a modesetting implementation, but are required while in an environment where the device's state is only managed by the DRM while X has the VT. --- linux-core/drmP.h | 3 + linux-core/drm_gem.c | 3 +- linux-core/i915_gem.c | 242 ++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 229 insertions(+), 19 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 8246f44a..6a7f28d1 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -1371,6 +1371,9 @@ drm_gem_init (struct drm_device *dev); void drm_gem_object_free (struct kref *kref); +struct drm_gem_object * +drm_gem_object_alloc(struct drm_device *dev, size_t size); + void drm_gem_object_handle_free (struct kref *kref); diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index b726e598..a8ecaf76 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -80,7 +80,7 @@ drm_gem_init(struct drm_device *dev) /** * Allocate a GEM object of the specified size with shmfs backing store */ -static struct drm_gem_object * +struct drm_gem_object * drm_gem_object_alloc(struct drm_device *dev, size_t size) { struct drm_gem_object *obj; @@ -117,6 +117,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) atomic_inc(&dev->object_count); return obj; } +EXPORT_SYMBOL(drm_gem_object_alloc); /** * Removes the mapping from handle to filp for this object. diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index d60a98f8..2564f418 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -702,14 +702,17 @@ i915_gem_object_get_page_list(struct drm_gem_object *obj) BUG_ON(obj_priv->page_list != NULL); obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *), DRM_MEM_DRIVER); - if (obj_priv->page_list == NULL) + if (obj_priv->page_list == NULL) { + DRM_ERROR("Faled to allocate page list\n"); return -ENOMEM; + } for (i = 0; i < page_count; i++) { obj_priv->page_list[i] = find_or_create_page(obj->filp->f_mapping, i, GFP_HIGHUSER); if (obj_priv->page_list[i] == NULL) { + DRM_ERROR("Failed to find_or_create_page()\n"); i915_gem_object_free_page_list(obj); return -ENOMEM; } @@ -758,14 +761,17 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) DRM_INFO("%s: GTT full, evicting something\n", __func__); #endif if (list_empty(&dev_priv->mm.inactive_list) && + list_empty(&dev_priv->mm.flushing_list) && list_empty(&dev_priv->mm.active_list)) { DRM_ERROR("GTT full, but LRU list empty\n"); return -ENOMEM; } ret = i915_gem_evict_something(dev); - if (ret != 0) + if (ret != 0) { + DRM_ERROR("Failed to evict a buffer\n"); return ret; + } goto search_free; } @@ -1383,6 +1389,7 @@ int i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) { + drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_execbuffer *args = data; struct drm_i915_gem_exec_object *exec_list = NULL; struct drm_gem_object **object_list = NULL; @@ -1423,6 +1430,12 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); + if (dev_priv->mm.suspended) { + DRM_ERROR("Execbuf while VT-switched.\n"); + mutex_unlock(&dev->struct_mutex); + return -EBUSY; + } + /* Zero the gloabl flush/invalidate flags. These * will be modified as each object is bound to the * gtt @@ -1559,6 +1572,37 @@ pre_mutex_err: return ret; } +int +i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) +{ + struct drm_device *dev = obj->dev; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + int ret; + + if (obj_priv->gtt_space == NULL) { + ret = i915_gem_object_bind_to_gtt(obj, alignment); + if (ret != 0) { + DRM_ERROR("Failure to bind in " + "i915_gem_pin_ioctl(): %d\n", + ret); + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + return ret; + } + } + + obj_priv->pin_count++; + return 0; +} + +void +i915_gem_object_unpin(struct drm_gem_object *obj) +{ + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + obj_priv->pin_count--; +} + int i915_gem_pin_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -1578,22 +1622,15 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, mutex_unlock(&dev->struct_mutex); return -EINVAL; } - obj_priv = obj->driver_private; - if (obj_priv->gtt_space == NULL) { - ret = i915_gem_object_bind_to_gtt(obj, - (unsigned) args->alignment); - if (ret != 0) { - DRM_ERROR("Failure to bind in " - "i915_gem_pin_ioctl(): %d\n", - ret); - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - return ret; - } + + ret = i915_gem_object_pin(obj, args->alignment); + if (ret != 0) { + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + return ret; } - obj_priv->pin_count++; args->offset = obj_priv->gtt_offset; drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); @@ -1607,7 +1644,6 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, { struct drm_i915_gem_pin *args = data; struct drm_gem_object *obj; - struct drm_i915_gem_object *obj_priv; mutex_lock(&dev->struct_mutex); @@ -1620,8 +1656,8 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, return -EINVAL; } - obj_priv = obj->driver_private; - obj_priv->pin_count--; + i915_gem_object_unpin(obj); + drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); return 0; @@ -1757,3 +1793,173 @@ i915_gem_lastclose(struct drm_device *dev) mutex_unlock(&dev->struct_mutex); } + +static int +i915_gem_init_ringbuffer(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + int ret; + + obj = drm_gem_object_alloc(dev, 128 * 1024); + if (obj == NULL) { + DRM_ERROR("Failed to allocate ringbuffer\n"); + return -ENOMEM; + } + obj_priv = obj->driver_private; + + ret = i915_gem_object_pin(obj, 4096); + if (ret != 0) + return ret; + + /* Set up the kernel mapping for the ring. */ + dev_priv->ring.Size = obj->size; + dev_priv->ring.tail_mask = obj->size - 1; + + dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset; + dev_priv->ring.map.size = obj->size; + dev_priv->ring.map.type = 0; + dev_priv->ring.map.flags = 0; + dev_priv->ring.map.mtrr = 0; + + drm_core_ioremap(&dev_priv->ring.map, dev); + if (dev_priv->ring.map.handle == NULL) { + DRM_ERROR("Failed to map ringbuffer.\n"); + memset(&dev_priv->ring, 0, sizeof(dev_priv->ring)); + drm_gem_object_unreference(obj); + return -EINVAL; + } + dev_priv->ring.ring_obj = obj; + dev_priv->ring.virtual_start = dev_priv->ring.map.handle; + + /* Stop the ring if it's running. */ + I915_WRITE(LP_RING + RING_LEN, 0); + I915_WRITE(LP_RING + RING_HEAD, 0); + I915_WRITE(LP_RING + RING_TAIL, 0); + I915_WRITE(LP_RING + RING_START, 0); + + /* Initialize the ring. */ + I915_WRITE(LP_RING + RING_START, obj_priv->gtt_offset); + I915_WRITE(LP_RING + RING_LEN, + ((obj->size - 4096) & RING_NR_PAGES) | + RING_NO_REPORT | + RING_VALID); + + /* Update our cache of the ring state */ + i915_kernel_lost_context(dev); + + return 0; +} + +static void +i915_gem_cleanup_ringbuffer(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + + if (dev_priv->ring.ring_obj == NULL) + return; + + drm_core_ioremapfree(&dev_priv->ring.map, dev); + + i915_gem_object_unpin(dev_priv->ring.ring_obj); + drm_gem_object_unreference(dev_priv->ring.ring_obj); + + memset(&dev_priv->ring, 0, sizeof(dev_priv->ring)); +} + +int +i915_gem_entervt_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + int ret; + + ret = i915_gem_init_ringbuffer(dev); + if (ret != 0) + return ret; + + mutex_lock(&dev->struct_mutex); + dev_priv->mm.suspended = 0; + mutex_unlock(&dev->struct_mutex); + return 0; +} + +/** Unbinds all objects that are on the given buffer list. */ +static int +i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head) +{ + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + int ret; + + while (!list_empty(head)) { + obj_priv = list_first_entry(head, + struct drm_i915_gem_object, + list); + obj = obj_priv->obj; + + if (obj_priv->pin_count != 0) { + DRM_ERROR("Pinned object in unbind list\n"); + mutex_unlock(&dev->struct_mutex); + return -EINVAL; + } + + ret = i915_gem_object_unbind(obj); + if (ret != 0) { + DRM_ERROR("Error unbinding object in LeaveVT: %d\n", + ret); + mutex_unlock(&dev->struct_mutex); + return ret; + } + } + + + return 0; +} + +int +i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + + mutex_lock(&dev->struct_mutex); + /* Hack! Don't let anybody do execbuf while we don't control the chip. + * We need to replace this with a semaphore, or something. + */ + dev_priv->mm.suspended = 1; + + /* Move all buffers out of the GTT. */ + i915_gem_evict_from_list(dev, &dev_priv->mm.active_list); + i915_gem_evict_from_list(dev, &dev_priv->mm.flushing_list); + i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list); + + /* Make sure the harware's idle. */ + while (!list_empty(&dev_priv->mm.request_list)) { + struct drm_i915_gem_request *request; + int ret; + + request = list_first_entry(&dev_priv->mm.request_list, + struct drm_i915_gem_request, + list); + + ret = i915_wait_request(dev, request->seqno); + if (ret != 0) { + DRM_ERROR("Error waiting for idle at LeaveVT: %d\n", + ret); + mutex_unlock(&dev->struct_mutex); + return ret; + } + } + + BUG_ON(!list_empty(&dev_priv->mm.active_list)); + BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); + BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); + + i915_gem_cleanup_ringbuffer(dev); + + mutex_unlock(&dev->struct_mutex); + + return 0; +} -- cgit v1.2.3 From dac3bcb414a21a77847c96740a1578f3488c774f Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 11 Jun 2008 11:28:20 -0700 Subject: [gem] Remove carefully-sprinkled i915_kernel_lost_context(). They are not unnecessary since the kernel's the only thing touching the ring. --- linux-core/i915_gem.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 2564f418..3d47ec1a 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1404,7 +1404,6 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", (int) args->buffers_ptr, args->buffer_count, args->batch_len); #endif - i915_kernel_lost_context(dev); /* Copy in the exec list from userland */ exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count, @@ -1614,7 +1613,6 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); - i915_kernel_lost_context(dev); obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) { DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n", @@ -1647,7 +1645,6 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); - i915_kernel_lost_context(dev); obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) { DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n", @@ -1711,7 +1708,6 @@ int i915_gem_init_object(struct drm_gem_object *obj) void i915_gem_free_object(struct drm_gem_object *obj) { - i915_kernel_lost_context(obj->dev); i915_gem_object_unbind(obj); drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); @@ -1729,7 +1725,6 @@ i915_gem_set_domain(struct drm_gem_object *obj, BUG_ON(!mutex_is_locked(&dev->struct_mutex)); drm_client_lock_take(dev, file_priv); - i915_kernel_lost_context(dev); ret = i915_gem_object_set_domain(obj, read_domains, write_domain); if (ret) { drm_client_lock_release(dev); -- cgit v1.2.3 From 2a35d857b35c9d30d073e2372aa302cd999321ba Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 11 Jun 2008 12:20:56 -0700 Subject: Remove override of drm module list in preparation for merge. --- linux-core/Makefile | 1 - 1 file changed, 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/Makefile b/linux-core/Makefile index fc32676f..3af6f370 100644 --- a/linux-core/Makefile +++ b/linux-core/Makefile @@ -30,7 +30,6 @@ # # make DRM_MODULES="r128 radeon" # -DRM_MODULES=i915 SHELL=/bin/sh -- cgit v1.2.3 From 2655005762b8915d5f44d1d1ee7e6c2eb34841d7 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 11 Jun 2008 14:42:40 -0700 Subject: [gem] Move potentially device-specific ioctls to the intel driver. This is the create (may want location flags), pread/pwrite/mmap (performance tuning hints), and set_domain (will 32 bits be enough for everyone?) ioctls. Left in the generic set are just flink/open/close. The 2D driver must be updated for this change, and API but not ABI is broken for 3D. The driver version is bumped to mark this. --- linux-core/drmP.h | 41 +------- linux-core/drm_drv.c | 5 - linux-core/drm_gem.c | 231 +---------------------------------------- linux-core/i915_drv.c | 2 - linux-core/i915_gem.c | 281 +++++++++++++++++++++++++++++++++++++++++--------- 5 files changed, 238 insertions(+), 322 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 6a7f28d1..48ff4b87 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -779,21 +779,6 @@ struct drm_driver { int (*gem_init_object) (struct drm_gem_object *obj); void (*gem_free_object) (struct drm_gem_object *obj); - /** - * Driver-specific callback to set memory domains from userspace - */ - int (*gem_set_domain) (struct drm_gem_object *obj, - struct drm_file *file_priv, - uint32_t read_domains, - uint32_t write_domain); - - /** - * Driver-specific callback to flush pwrite through chipset - */ - int (*gem_flush_pwrite) (struct drm_gem_object *obj, - uint64_t offset, - uint64_t size); - struct drm_fence_driver *fence_driver; struct drm_bo_driver *bo_driver; @@ -1390,6 +1375,11 @@ static inline void drm_gem_object_unreference(struct drm_gem_object *obj) kref_put (&obj->refcount, drm_gem_object_free); } +int +drm_gem_handle_create(struct drm_file *file_priv, + struct drm_gem_object *obj, + int *handlep); + static inline void drm_gem_object_handle_reference (struct drm_gem_object *obj) { drm_gem_object_reference (obj); @@ -1413,37 +1403,16 @@ static inline void drm_gem_object_handle_unreference (struct drm_gem_object *obj struct drm_gem_object * drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, int handle); -int drm_gem_create_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); int drm_gem_close_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); -int drm_gem_pread_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); -int drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); -int drm_gem_mmap_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); int drm_gem_flink_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int drm_gem_open_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); -int drm_gem_set_domain_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv); void drm_gem_open(struct drm_device *dev, struct drm_file *file_private); void drm_gem_release(struct drm_device *dev, struct drm_file *file_private); - -/* - * Given the new read/write domains for an object, - * compute the invalidate/flush domains for the whole device. - * - */ -int drm_gem_object_set_domain (struct drm_gem_object *object, - uint32_t read_domains, - uint32_t write_domains); - - extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev); extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev); diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index edc1f057..980752c2 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -151,14 +151,9 @@ static struct drm_ioctl_desc drm_ioctls[] = { DRM_IOCTL_DEF(DRM_IOCTL_MM_INFO, drm_mm_info_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_GEM_CREATE, drm_gem_create_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_GEM_PREAD, drm_gem_pread_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_GEM_PWRITE, drm_gem_pwrite_ioctl, 0), - DRM_IOCTL_DEF(DRM_IOCTL_GEM_MMAP, drm_gem_mmap_ioctl, 0), DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH), DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH), - DRM_IOCTL_DEF(DRM_IOCTL_GEM_SET_DOMAIN, drm_gem_set_domain_ioctl, DRM_AUTH), }; #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls ) diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index a8ecaf76..47b55434 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -99,15 +99,6 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) kref_init(&obj->refcount); kref_init(&obj->handlecount); obj->size = size; - - /* - * We've just allocated pages from the kernel, - * so they've just been written by the CPU with - * zeros. They'll need to be clflushed before we - * use them with the GPU. - */ - obj->write_domain = DRM_GEM_DOMAIN_CPU; - obj->read_domains = DRM_GEM_DOMAIN_CPU; if (dev->driver->gem_init_object != NULL && dev->driver->gem_init_object(obj) != 0) { fput(obj->filp); @@ -163,7 +154,7 @@ drm_gem_handle_delete(struct drm_file *filp, int handle) * to the object, which includes a regular reference count. Callers * will likely want to dereference the object afterwards. */ -static int +int drm_gem_handle_create(struct drm_file *file_priv, struct drm_gem_object *obj, int *handlep) @@ -191,6 +182,7 @@ again: drm_gem_object_handle_reference(obj); return 0; } +EXPORT_SYMBOL(drm_gem_handle_create); /** Returns a reference to the object named by the handle. */ struct drm_gem_object * @@ -216,40 +208,6 @@ drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, } EXPORT_SYMBOL(drm_gem_object_lookup); -/** - * Creates a new mm object and returns a handle to it. - */ -int -drm_gem_create_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_gem_create *args = data; - struct drm_gem_object *obj; - int handle, ret; - - if (!(dev->driver->driver_features & DRIVER_GEM)) - return -ENODEV; - - args->size = roundup(args->size, PAGE_SIZE); - - /* Allocate the new object */ - obj = drm_gem_object_alloc(dev, args->size); - if (obj == NULL) - return -ENOMEM; - - ret = drm_gem_handle_create(file_priv, obj, &handle); - mutex_lock(&dev->struct_mutex); - drm_gem_object_handle_unreference(obj); - mutex_unlock(&dev->struct_mutex); - - if (ret) - return ret; - - args->handle = handle; - - return 0; -} - /** * Releases the handle to an mm object. */ @@ -268,158 +226,6 @@ drm_gem_close_ioctl(struct drm_device *dev, void *data, return ret; } -/** - * Reads data from the object referenced by handle. - * - * On error, the contents of *data are undefined. - */ -int -drm_gem_pread_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_gem_pread *args = data; - struct drm_gem_object *obj; - ssize_t read; - loff_t offset; - int ret; - - if (!(dev->driver->driver_features & DRIVER_GEM)) - return -ENODEV; - - obj = drm_gem_object_lookup(dev, file_priv, args->handle); - if (obj == NULL) - return -EINVAL; - - mutex_lock(&dev->struct_mutex); - if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain(obj, file_priv, - DRM_GEM_DOMAIN_CPU, - 0); - if (ret) { - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - return ret; - } - } - offset = args->offset; - - read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr, - args->size, &offset); - if (read != args->size) { - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - if (read < 0) - return read; - else - return -EINVAL; - } - - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - - return 0; -} - -/** - * Maps the contents of an object, returning the address it is mapped - * into. - * - * While the mapping holds a reference on the contents of the object, it doesn't - * imply a ref on the object itself. - */ -int -drm_gem_mmap_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_gem_mmap *args = data; - struct drm_gem_object *obj; - loff_t offset; - unsigned long addr; - - if (!(dev->driver->driver_features & DRIVER_GEM)) - return -ENODEV; - - obj = drm_gem_object_lookup(dev, file_priv, args->handle); - if (obj == NULL) - return -EINVAL; - - offset = args->offset; - - down_write(¤t->mm->mmap_sem); - addr = do_mmap(obj->filp, 0, args->size, - PROT_READ | PROT_WRITE, MAP_SHARED, - args->offset); - up_write(¤t->mm->mmap_sem); - mutex_lock(&dev->struct_mutex); - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - if (IS_ERR((void *)addr)) - return addr; - - args->addr_ptr = (uint64_t) addr; - - return 0; -} - -/** - * Writes data to the object referenced by handle. - * - * On error, the contents of the buffer that were to be modified are undefined. - */ -int -drm_gem_pwrite_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_gem_pwrite *args = data; - struct drm_gem_object *obj; - ssize_t written; - loff_t offset; - int ret; - - if (!(dev->driver->driver_features & DRIVER_GEM)) - return -ENODEV; - - obj = drm_gem_object_lookup(dev, file_priv, args->handle); - if (obj == NULL) - return -EINVAL; - - mutex_lock(&dev->struct_mutex); - if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain(obj, file_priv, - DRM_GEM_DOMAIN_CPU, - DRM_GEM_DOMAIN_CPU); - if (ret) { - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - return ret; - } - } - offset = args->offset; - - written = vfs_write(obj->filp, - (char __user *)(uintptr_t) args->data_ptr, - args->size, &offset); - - if (written != args->size) { - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - if (written < 0) - return written; - else - return -EINVAL; - } - - if (dev->driver->gem_flush_pwrite) - dev->driver->gem_flush_pwrite(obj, - args->offset, - args->size); - - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - - return 0; -} - /** * Create a global name for an object, returning the name. * @@ -511,39 +317,6 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data, return 0; } -/** - * Called when user space prepares to use an object - */ -int -drm_gem_set_domain_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) -{ - struct drm_gem_set_domain *args = data; - struct drm_gem_object *obj; - int ret; - - if (!(dev->driver->driver_features & DRIVER_GEM)) - return -ENODEV; - - obj = drm_gem_object_lookup(dev, file_priv, args->handle); - if (obj == NULL) - return -EINVAL; - - mutex_lock(&dev->struct_mutex); - if (dev->driver->gem_set_domain) { - ret = dev->driver->gem_set_domain(obj, file_priv, - args->read_domains, - args->write_domain); - } else { - obj->read_domains = args->read_domains; - obj->write_domain = args->write_domain; - ret = 0; - } - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - return ret; -} - /** * Called at device open time, sets up the structure for handling refcounting * of mm objects. diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index 3f246a08..8f51cd4d 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -588,8 +588,6 @@ static struct drm_driver driver = { .ioctls = i915_ioctls, .gem_init_object = i915_gem_init_object, .gem_free_object = i915_gem_free_object, - .gem_set_domain = i915_gem_set_domain, - .gem_flush_pwrite = i915_gem_flush_pwrite, .fops = { .owner = THIS_MODULE, .open = drm_open, diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 3d47ec1a..e51de316 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -40,6 +40,11 @@ static int i915_gem_object_set_domain(struct drm_gem_object *obj, uint32_t read_domains, uint32_t write_domain); +int +i915_gem_set_domain(struct drm_gem_object *obj, + struct drm_file *file_priv, + uint32_t read_domains, + uint32_t write_domain); int i915_gem_init_ioctl(struct drm_device *dev, void *data, @@ -65,6 +70,199 @@ i915_gem_init_ioctl(struct drm_device *dev, void *data, return 0; } + +/** + * Creates a new mm object and returns a handle to it. + */ +int +i915_gem_create_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_create *args = data; + struct drm_gem_object *obj; + int handle, ret; + + args->size = roundup(args->size, PAGE_SIZE); + + /* Allocate the new object */ + obj = drm_gem_object_alloc(dev, args->size); + if (obj == NULL) + return -ENOMEM; + + ret = drm_gem_handle_create(file_priv, obj, &handle); + mutex_lock(&dev->struct_mutex); + drm_gem_object_handle_unreference(obj); + mutex_unlock(&dev->struct_mutex); + + if (ret) + return ret; + + args->handle = handle; + + return 0; +} + +/** + * Reads data from the object referenced by handle. + * + * On error, the contents of *data are undefined. + */ +int +i915_gem_pread_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_pread *args = data; + struct drm_gem_object *obj; + ssize_t read; + loff_t offset; + int ret; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + mutex_lock(&dev->struct_mutex); + ret = i915_gem_set_domain(obj, file_priv, + I915_GEM_DOMAIN_CPU, 0); + if (ret) { + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + return ret; + } + offset = args->offset; + + read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr, + args->size, &offset); + if (read != args->size) { + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + if (read < 0) + return read; + else + return -EINVAL; + } + + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + + return 0; +} + +/** + * Writes data to the object referenced by handle. + * + * On error, the contents of the buffer that were to be modified are undefined. + */ +int +i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_pwrite *args = data; + struct drm_gem_object *obj; + ssize_t written; + loff_t offset; + int ret; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + mutex_lock(&dev->struct_mutex); + ret = i915_gem_set_domain(obj, file_priv, + I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU); + if (ret) { + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + return ret; + } + offset = args->offset; + + written = vfs_write(obj->filp, + (char __user *)(uintptr_t) args->data_ptr, + args->size, &offset); + + if (written != args->size) { + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + if (written < 0) + return written; + else + return -EINVAL; + } + + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + + return 0; +} + +/** + * Called when user space prepares to use an object + */ +int +i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_set_domain *args = data; + struct drm_gem_object *obj; + int ret; + + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + mutex_lock(&dev->struct_mutex); + ret = i915_gem_set_domain(obj, file_priv, + args->read_domains, args->write_domain); + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + return ret; +} + +/** + * Maps the contents of an object, returning the address it is mapped + * into. + * + * While the mapping holds a reference on the contents of the object, it doesn't + * imply a ref on the object itself. + */ +int +i915_gem_mmap_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_mmap *args = data; + struct drm_gem_object *obj; + loff_t offset; + unsigned long addr; + + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + + offset = args->offset; + + down_write(¤t->mm->mmap_sem); + addr = do_mmap(obj->filp, 0, args->size, + PROT_READ | PROT_WRITE, MAP_SHARED, + args->offset); + up_write(¤t->mm->mmap_sem); + mutex_lock(&dev->struct_mutex); + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + if (IS_ERR((void *)addr)) + return addr; + + args->addr_ptr = (uint64_t) addr; + + return 0; +} + static void i915_gem_object_free_page_list(struct drm_gem_object *obj) { @@ -187,7 +385,7 @@ i915_retire_commands(struct drm_device *dev) /* The sampler always gets flushed on i965 (sigh) */ if (IS_I965G(dev)) - flush_domains |= DRM_GEM_DOMAIN_I915_SAMPLER; + flush_domains |= I915_GEM_DOMAIN_SAMPLER; BEGIN_LP_RING(2); OUT_RING(cmd); OUT_RING(0); /* noop */ @@ -377,51 +575,51 @@ i915_gem_flush(struct drm_device *dev, invalidate_domains, flush_domains); #endif - if (flush_domains & DRM_GEM_DOMAIN_CPU) + if (flush_domains & I915_GEM_DOMAIN_CPU) drm_agp_chipset_flush(dev); - if ((invalidate_domains|flush_domains) & ~DRM_GEM_DOMAIN_CPU) { + if ((invalidate_domains|flush_domains) & ~I915_GEM_DOMAIN_CPU) { /* * read/write caches: * - * DRM_GEM_DOMAIN_I915_RENDER is always invalidated, but is + * I915_GEM_DOMAIN_RENDER is always invalidated, but is * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is * also flushed at 2d versus 3d pipeline switches. * * read-only caches: * - * DRM_GEM_DOMAIN_I915_SAMPLER is flushed on pre-965 if + * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if * MI_READ_FLUSH is set, and is always flushed on 965. * - * DRM_GEM_DOMAIN_I915_COMMAND may not exist? + * I915_GEM_DOMAIN_COMMAND may not exist? * - * DRM_GEM_DOMAIN_I915_INSTRUCTION, which exists on 965, is + * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is * invalidated when MI_EXE_FLUSH is set. * - * DRM_GEM_DOMAIN_I915_VERTEX, which exists on 965, is + * I915_GEM_DOMAIN_VERTEX, which exists on 965, is * invalidated with every MI_FLUSH. * * TLBs: * - * On 965, TLBs associated with DRM_GEM_DOMAIN_I915_COMMAND - * and DRM_GEM_DOMAIN_CPU in are invalidated at PTE write and - * DRM_GEM_DOMAIN_I915_RENDER and DRM_GEM_DOMAIN_I915_SAMPLER + * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND + * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and + * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER * are flushed at any MI_FLUSH. */ cmd = CMD_MI_FLUSH | MI_NO_WRITE_FLUSH; if ((invalidate_domains|flush_domains) & - DRM_GEM_DOMAIN_I915_RENDER) + I915_GEM_DOMAIN_RENDER) cmd &= ~MI_NO_WRITE_FLUSH; if (!IS_I965G(dev)) { /* * On the 965, the sampler cache always gets flushed * and this bit is reserved. */ - if (invalidate_domains & DRM_GEM_DOMAIN_I915_SAMPLER) + if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER) cmd |= MI_READ_FLUSH; } - if (invalidate_domains & DRM_GEM_DOMAIN_I915_INSTRUCTION) + if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION) cmd |= MI_EXE_FLUSH; #if WATCH_EXEC @@ -448,7 +646,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) /* If there are writes queued to the buffer, flush and * create a new seqno to wait for. */ - if (obj->write_domain & ~(DRM_GEM_DOMAIN_CPU)) { + if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU)) { uint32_t write_domain = obj->write_domain; #if WATCH_BUF DRM_INFO("%s: flushing object %p from write domain %08x\n", @@ -503,8 +701,8 @@ i915_gem_object_unbind(struct drm_gem_object *obj) * also ensure that all pending GPU writes are finished * before we unbind. */ - ret = i915_gem_object_set_domain (obj, DRM_GEM_DOMAIN_CPU, - DRM_GEM_DOMAIN_CPU); + ret = i915_gem_object_set_domain (obj, I915_GEM_DOMAIN_CPU, + I915_GEM_DOMAIN_CPU); if (ret) return ret; @@ -805,8 +1003,8 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) * wasn't in the GTT, there shouldn't be any way it could have been in * a GPU cache */ - BUG_ON(obj->read_domains & ~DRM_GEM_DOMAIN_CPU); - BUG_ON(obj->write_domain & ~DRM_GEM_DOMAIN_CPU); + BUG_ON(obj->read_domains & ~I915_GEM_DOMAIN_CPU); + BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); return 0; } @@ -973,7 +1171,7 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, * stale data. That is, any new read domains. */ invalidate_domains |= read_domains & ~obj->read_domains; - if ((flush_domains | invalidate_domains) & DRM_GEM_DOMAIN_CPU) { + if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) { #if WATCH_BUF DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n", __func__, flush_domains, invalidate_domains); @@ -983,8 +1181,8 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, * then pause for rendering so that the GPU caches will be * flushed before the cpu cache is invalidated */ - if ((invalidate_domains & DRM_GEM_DOMAIN_CPU) && - (flush_domains & ~DRM_GEM_DOMAIN_CPU)) { + if ((invalidate_domains & I915_GEM_DOMAIN_CPU) && + (flush_domains & ~I915_GEM_DOMAIN_CPU)) { ret = i915_gem_object_wait_rendering(obj); if (ret) return ret; @@ -1225,7 +1423,7 @@ i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, /* As we're writing through the gtt, flush * any CPU writes before we write the relocations */ - if (obj->write_domain & DRM_GEM_DOMAIN_CPU) { + if (obj->write_domain & I915_GEM_DOMAIN_CPU) { i915_gem_clflush_object(obj); drm_agp_chipset_flush(dev); obj->write_domain = 0; @@ -1466,7 +1664,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, /* Set the pending read domains for the batch buffer to COMMAND */ batch_obj = object_list[args->buffer_count-1]; - batch_obj->pending_read_domains = DRM_GEM_DOMAIN_I915_COMMAND; + batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND; batch_obj->pending_write_domain = 0; for (i = 0; i < args->buffer_count; i++) { @@ -1700,6 +1898,15 @@ int i915_gem_init_object(struct drm_gem_object *obj) if (obj_priv == NULL) return -ENOMEM; + /* + * We've just allocated pages from the kernel, + * so they've just been written by the CPU with + * zeros. They'll need to be clflushed before we + * use them with the GPU. + */ + obj->write_domain = I915_GEM_DOMAIN_CPU; + obj->read_domains = I915_GEM_DOMAIN_CPU; + obj->driver_private = obj_priv; obj_priv->obj = obj; INIT_LIST_HEAD(&obj_priv->list); @@ -1735,32 +1942,6 @@ i915_gem_set_domain(struct drm_gem_object *obj, return 0; } -int -i915_gem_flush_pwrite(struct drm_gem_object *obj, - uint64_t offset, uint64_t size) -{ -#if 0 - struct drm_device *dev = obj->dev; - struct drm_i915_gem_object *obj_priv = obj->driver_private; - - /* - * For writes much less than the size of the object and - * which are already pinned in memory, do the flush right now - */ - - if ((size < obj->size >> 1) && obj_priv->page_list != NULL) { - unsigned long first_page = offset / PAGE_SIZE; - unsigned long beyond_page = roundup(offset + size, PAGE_SIZE) / PAGE_SIZE; - - drm_ttm_cache_flush(obj_priv->page_list + first_page, - beyond_page - first_page); - drm_agp_chipset_flush(dev); - obj->write_domain = 0; - } -#endif - return 0; -} - void i915_gem_lastclose(struct drm_device *dev) { -- cgit v1.2.3 From 846d792ac10c4b2738bb5ff59e56df168b9921ff Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 11 Jun 2008 15:51:17 -0700 Subject: [gem] Another round of cleanups from checkpatch.pl --- linux-core/i915_gem.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index e51de316..d608cb03 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -547,8 +547,8 @@ i915_wait_request(struct drm_device *dev, uint32_t seqno) i915_user_irq_off(dev_priv); } if (ret) - DRM_ERROR ("%s returns %d (awaiting %d at %d)\n", - __func__, ret, seqno, i915_get_gem_seqno(dev)); + DRM_ERROR("%s returns %d (awaiting %d at %d)\n", + __func__, ret, seqno, i915_get_gem_seqno(dev)); /* Directly dispatch request retiring. While we have the work queue * to handle this, the waiter on a request often wants an associated @@ -701,8 +701,8 @@ i915_gem_object_unbind(struct drm_gem_object *obj) * also ensure that all pending GPU writes are finished * before we unbind. */ - ret = i915_gem_object_set_domain (obj, I915_GEM_DOMAIN_CPU, - I915_GEM_DOMAIN_CPU); + ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU, + I915_GEM_DOMAIN_CPU); if (ret) return ret; @@ -1242,7 +1242,7 @@ i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle) int bad_count = 0; DRM_INFO("%s: checking coherency of object %p@0x%08x (%d, %dkb):\n", - __FUNCTION__, obj, obj_priv->gtt_offset, handle, + __func__, obj, obj_priv->gtt_offset, handle, obj->size / 1024); gtt_mapping = ioremap(dev->agp->base + obj_priv->gtt_offset, @@ -1301,9 +1301,7 @@ i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle) #endif /** - * Bind an object to the GTT and evaluate the relocations landing in it - * - * + * Bind an object to the GTT and evaluate the relocations landing in it. */ static int i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, @@ -1567,7 +1565,8 @@ i915_gem_ring_throttle(struct drm_device *dev) list); /* Break out if we're close enough. */ - if ((long) (jiffies - request->emitted_jiffies) <= (20 * HZ) / 1000) { + if ((long) (jiffies - request->emitted_jiffies) <= + (20 * HZ) / 1000) { mutex_unlock(&dev->struct_mutex); return 0; } @@ -1877,7 +1876,7 @@ i915_gem_busy_ioctl(struct drm_device *dev, void *data, obj_priv = obj->driver_private; args->busy = obj_priv->active; - + drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); return 0; -- cgit v1.2.3 From b2606e325ac02782297def5ce27028c7fe2287c8 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 11 Jun 2008 16:19:23 -0700 Subject: [gem] Remove the drm_client_lock_take in set_domain. We no longer need to use it to protect against shared ringbuffer access. --- linux-core/drmP.h | 3 --- linux-core/drm_lock.c | 56 --------------------------------------------------- linux-core/i915_gem.c | 7 ++----- 3 files changed, 2 insertions(+), 64 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 48ff4b87..02826c8d 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -1169,9 +1169,6 @@ extern int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context); extern int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context); extern void drm_idlelock_take(struct drm_lock_data *lock_data); extern void drm_idlelock_release(struct drm_lock_data *lock_data); -extern int drm_client_lock_take(struct drm_device *dev, - struct drm_file *file_priv); -extern void drm_client_lock_release(struct drm_device *dev); /* * These are exported to drivers so that they can implement fencing using diff --git a/linux-core/drm_lock.c b/linux-core/drm_lock.c index e6eae42b..e8496a25 100644 --- a/linux-core/drm_lock.c +++ b/linux-core/drm_lock.c @@ -375,62 +375,6 @@ void drm_idlelock_release(struct drm_lock_data *lock_data) } EXPORT_SYMBOL(drm_idlelock_release); -/** - * Takes the lock on behalf of the client if needed, using the kernel context. - * - * This allows us to hide the hardware lock when it's required for protection - * of data structures (such as command ringbuffer) shared with the X Server, and - * a way for us to transition to lockless for those requests when the X Server - * stops accessing the ringbuffer directly, without having to update the - * other userland clients. - */ -int drm_client_lock_take(struct drm_device *dev, struct drm_file *file_priv) -{ - int ret; - unsigned long irqflags; - - /* If the client has the lock, we're already done. */ - if (drm_i_have_hw_lock(dev, file_priv)) - return 0; - - mutex_unlock (&dev->struct_mutex); - /* Client doesn't hold the lock. Block taking the lock with the kernel - * context on behalf of the client, and return whether we were - * successful. - */ - spin_lock_irqsave(&dev->lock.spinlock, irqflags); - dev->lock.user_waiters++; - spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); - ret = wait_event_interruptible(dev->lock.lock_queue, - drm_lock_take(&dev->lock, - DRM_KERNEL_CONTEXT)); - spin_lock_irqsave(&dev->lock.spinlock, irqflags); - dev->lock.user_waiters--; - if (ret != 0) { - spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); - } else { - dev->lock.file_priv = file_priv; - dev->lock.lock_time = jiffies; - dev->lock.kernel_held = 1; - file_priv->lock_count++; - spin_unlock_irqrestore(&dev->lock.spinlock, irqflags); - } - mutex_lock (&dev->struct_mutex); - return ret; -} -EXPORT_SYMBOL(drm_client_lock_take); - -void drm_client_lock_release(struct drm_device *dev) -{ - if (dev->lock.kernel_held) { - dev->lock.kernel_held = 0; - dev->lock.file_priv = NULL; - drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT); - } -} -EXPORT_SYMBOL(drm_client_lock_release); - - int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv) { diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index d608cb03..030ecf1d 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1930,14 +1930,11 @@ i915_gem_set_domain(struct drm_gem_object *obj, BUG_ON(!mutex_is_locked(&dev->struct_mutex)); - drm_client_lock_take(dev, file_priv); ret = i915_gem_object_set_domain(obj, read_domains, write_domain); - if (ret) { - drm_client_lock_release(dev); + if (ret) return ret; - } i915_gem_dev_set_domain(obj->dev); - drm_client_lock_release(dev); + return 0; } -- cgit v1.2.3 From c892e26bdfcacfe7213085a08dd82e2cb7faa003 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 13 Jun 2008 09:49:05 -0700 Subject: [gem] Don't require the lock in execbuf now that it's not needed for the ring. --- linux-core/i915_gem.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 030ecf1d..634b4f6c 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1595,8 +1595,6 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, uint64_t exec_offset; uint32_t seqno, flush_domains; - LOCK_TEST_WITH_RETURN(dev, file_priv); - #if WATCH_EXEC DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", (int) args->buffers_ptr, args->buffer_count, args->batch_len); -- cgit v1.2.3 From f378319b5627d84ca821c8e6a9f2fee612119477 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 10 Jun 2008 17:53:32 -0700 Subject: Use /bin/pwd instead of trusting shell built-in --- linux-core/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/Makefile b/linux-core/Makefile index 3af6f370..55dfb77c 100644 --- a/linux-core/Makefile +++ b/linux-core/Makefile @@ -116,7 +116,7 @@ V := $(shell if [ -f $(BOOTVERSION_PREFIX)version.h ]; then \ ifeq ($(V),"$(RUNNING_REL)") HEADERFROMBOOT := 1 -GETCONFIG := MAKEFILES=$(shell pwd)/.config +GETCONFIG := MAKEFILES=$(shell /bin/pwd)/.config HAVECONFIG := y endif @@ -163,7 +163,7 @@ endif all: modules modules: includes - +make -C $(LINUXDIR) $(GETCONFIG) SUBDIRS=`pwd` DRMSRCDIR=`pwd` modules + +make -C $(LINUXDIR) $(GETCONFIG) SUBDIRS=`/bin/pwd` DRMSRCDIR=`/bin/pwd` modules ifeq ($(HEADERFROMBOOT),1) @@ -239,7 +239,7 @@ drmstat: drmstat.c $(CC) $(PRGCFLAGS) $< -o $@ $(DRMSTATLIBS) install: - make -C $(LINUXDIR) $(GETCONFIG) SUBDIRS=`pwd` DRMSRCDIR=`pwd` modules_install + make -C $(LINUXDIR) $(GETCONFIG) SUBDIRS=`/bin/pwd` DRMSRCDIR=`/bin/pwd` modules_install else -- cgit v1.2.3 From e5364914ac2b785f9d806c72fff8d2ae914cad61 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 10 Jun 2008 18:11:15 -0700 Subject: [intel-gem] Reorder i915_add_request to schedule work last i915_add_request was calling schedule_delayed_work before adding the request to the list; it makes more sense to do that last. --- linux-core/i915_gem.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 634b4f6c..a14781f6 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -333,6 +333,7 @@ i915_add_request(struct drm_device *dev, uint32_t flush_domains) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_request *request; uint32_t seqno; + int was_empty; RING_LOCALS; request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER); @@ -360,11 +361,11 @@ i915_add_request(struct drm_device *dev, uint32_t flush_domains) request->seqno = seqno; request->emitted_jiffies = jiffies; request->flush_domains = flush_domains; - if (list_empty(&dev_priv->mm.request_list)) - mod_timer(&dev_priv->mm.retire_timer, jiffies + HZ); - + was_empty = list_empty(&dev_priv->mm.request_list); list_add_tail(&request->list, &dev_priv->mm.request_list); + if (was_empty) + schedule_delayed_work (&dev_priv->mm.retire_work, HZ); return seqno; } -- cgit v1.2.3 From 462af73149b9286a74b95b9cda5e4224ebe0dd87 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 09:19:30 -0700 Subject: [intel-gem] Use a delayed_work instead of a timer + work_struct We want request retirement to occur about once a second when the request queue is non-empty. This was done with a timer that queued a work_struct, using a delayed_work instead makes a lot more sense. --- linux-core/i915_gem.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index a14781f6..115f8e75 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -503,28 +503,19 @@ i915_gem_retire_requests(struct drm_device *dev) } void -i915_gem_retire_timeout(unsigned long data) -{ - struct drm_device *dev = (struct drm_device *) data; - drm_i915_private_t *dev_priv = dev->dev_private; - - schedule_work(&dev_priv->mm.retire_task); -} - -void -i915_gem_retire_handler(struct work_struct *work) +i915_gem_retire_work_handler(struct work_struct *work) { drm_i915_private_t *dev_priv; struct drm_device *dev; dev_priv = container_of(work, drm_i915_private_t, - mm.retire_task); + mm.retire_work.work); dev = dev_priv->dev; mutex_lock(&dev->struct_mutex); i915_gem_retire_requests(dev); if (!list_empty(&dev_priv->mm.request_list)) - mod_timer(&dev_priv->mm.retire_timer, jiffies + HZ); + schedule_delayed_work (&dev_priv->mm.retire_work, HZ); mutex_unlock(&dev->struct_mutex); } -- cgit v1.2.3 From 3762c9ea6754763694b1de4df3acd9dc37247f87 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 10:27:39 -0700 Subject: [intel] Enable MSI for i915 IRQ --- linux-core/i915_drv.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index 8f51cd4d..89f089b0 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -560,6 +560,7 @@ static int i915_resume(struct drm_device *dev) } static int probe(struct pci_dev *pdev, const struct pci_device_id *ent); +static void remove(struct pci_dev *pdev); static struct drm_driver driver = { /* don't use mtrr's here, the Xserver or user space app should * deal with them for intel hardware. @@ -604,7 +605,7 @@ static struct drm_driver driver = { .name = DRIVER_NAME, .id_table = pciidlist, .probe = probe, - .remove = __devexit_p(drm_cleanup_pci), + .remove = remove, }, #ifdef I915_HAVE_FENCE .fence_driver = &i915_fence_driver, @@ -622,7 +623,19 @@ static struct drm_driver driver = { static int probe(struct pci_dev *pdev, const struct pci_device_id *ent) { - return drm_get_dev(pdev, ent, &driver); + int ret; + + (void) pci_enable_msi(pdev); + ret = drm_get_dev(pdev, ent, &driver); + if (ret && pdev->msi_enabled) + pci_disable_msi(pdev); + return ret; +} +static void remove(struct pci_dev *pdev) +{ + if (pdev->msi_enabled) + pci_disable_msi(pdev); + drm_cleanup_pci(pdev); } static int __init i915_init(void) -- cgit v1.2.3 From 6b2cba1ecc5f9f289b5d91e229b7f7b0999bee5b Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 11:33:27 -0700 Subject: [intel-gem] evict_something was failing when wait_request freed objects When i915_wait_request clears object from the active list, it may end up freeing them and not moving them to the inactive list. This ends up unbinding objects from the GTT without there ever being new objects visible to i915_gem_evict_something on the inactive list. As the only success condition required the presence of objects on the inactive list, this would falsely assume that no GTT space had been made available, and end up returning -ENOMEM to the application. --- linux-core/i915_gem.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 115f8e75..b114192d 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -805,7 +805,7 @@ i915_gem_evict_something(struct drm_device *dev) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_gem_object *obj; struct drm_i915_gem_object *obj_priv; - int ret; + int ret = 0; for (;;) { /* If there's an inactive buffer available now, grab it @@ -817,6 +817,13 @@ i915_gem_evict_something(struct drm_device *dev) list); obj = obj_priv->obj; BUG_ON(obj_priv->pin_count != 0); +#if WATCH_LRU + DRM_INFO("%s: evicting %p\n", __func__, obj); +#endif + BUG_ON(obj_priv->active); + + /* Wait on the rendering and unbind the buffer. */ + ret = i915_gem_object_unbind(obj); break; } @@ -826,17 +833,21 @@ i915_gem_evict_something(struct drm_device *dev) */ if (!list_empty(&dev_priv->mm.request_list)) { struct drm_i915_gem_request *request; - int ret; request = list_first_entry(&dev_priv->mm.request_list, struct drm_i915_gem_request, list); ret = i915_wait_request(dev, request->seqno); - if (ret != 0) - return ret; - - continue; + + /* if waiting caused an object to become inactive, + * then loop around and wait for it. Otherwise, we + * assume that waiting freed and unbound something, + * so there should now be some space in the GTT + */ + if (!list_empty(&dev_priv->mm.inactive_list)) + continue; + break; } /* If we didn't have anything on the request list but there @@ -859,21 +870,15 @@ i915_gem_evict_something(struct drm_device *dev) continue; } + DRM_ERROR("inactive empty %d request empty %d flushing empty %d\n", + list_empty(&dev_priv->mm.inactive_list), + list_empty(&dev_priv->mm.request_list), + list_empty(&dev_priv->mm.flushing_list)); /* If we didn't do any of the above, there's nothing to be done * and we just can't fit it in. */ return -ENOMEM; } - -#if WATCH_LRU - DRM_INFO("%s: evicting %p\n", __func__, obj); -#endif - - BUG_ON(obj_priv->active); - - /* Wait on the rendering and unbind the buffer. */ - ret = i915_gem_object_unbind(obj); - return ret; } @@ -959,7 +964,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) ret = i915_gem_evict_something(dev); if (ret != 0) { - DRM_ERROR("Failed to evict a buffer\n"); + DRM_ERROR("Failed to evict a buffer %d\n", ret); return ret; } goto search_free; -- cgit v1.2.3 From ced9ebf64543b4d64a38feee3293040af953acc0 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 12:06:13 -0700 Subject: [intel-gem] throttle based on frames rather than time. Reduces jitter. Record the last execbuffer sequence for each client. Record that sequence in the throttle ioctl as the 'throttle sequence'. Wait for the last throttle sequence in the throttle ioctl. --- linux-core/i915_drv.c | 3 +++ linux-core/i915_gem.c | 34 ++++++++++------------------------ 2 files changed, 13 insertions(+), 24 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index 89f089b0..012ca822 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -561,6 +561,7 @@ static int i915_resume(struct drm_device *dev) static int probe(struct pci_dev *pdev, const struct pci_device_id *ent); static void remove(struct pci_dev *pdev); + static struct drm_driver driver = { /* don't use mtrr's here, the Xserver or user space app should * deal with them for intel hardware. @@ -571,8 +572,10 @@ static struct drm_driver driver = { .load = i915_driver_load, .unload = i915_driver_unload, .firstopen = i915_driver_firstopen, + .open = i915_driver_open, .lastclose = i915_driver_lastclose, .preclose = i915_driver_preclose, + .postclose = i915_driver_postclose, .suspend = i915_suspend, .resume = i915_resume, .device_is_agp = i915_driver_device_is_agp, diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index b114192d..c2d1fab6 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1548,33 +1548,17 @@ i915_dispatch_gem_execbuffer(struct drm_device *dev, * relatively low latency when blocking on a particular request to finish. */ static int -i915_gem_ring_throttle(struct drm_device *dev) +i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv) { - drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv; int ret = 0; + uint32_t seqno; mutex_lock(&dev->struct_mutex); - while (!list_empty(&dev_priv->mm.request_list)) { - struct drm_i915_gem_request *request; - - request = list_first_entry(&dev_priv->mm.request_list, - struct drm_i915_gem_request, - list); - - /* Break out if we're close enough. */ - if ((long) (jiffies - request->emitted_jiffies) <= - (20 * HZ) / 1000) { - mutex_unlock(&dev->struct_mutex); - return 0; - } - - /* Wait on the last request if not. */ - ret = i915_wait_request(dev, request->seqno); - if (ret != 0) { - mutex_unlock(&dev->struct_mutex); - return ret; - } - } + seqno = i915_file_priv->mm.last_gem_throttle_seqno; + i915_file_priv->mm.last_gem_throttle_seqno = i915_file_priv->mm.last_gem_seqno; + if (seqno) + ret = i915_wait_request(dev, seqno); mutex_unlock(&dev->struct_mutex); return ret; } @@ -1584,6 +1568,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv) { drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv; struct drm_i915_gem_execbuffer *args = data; struct drm_i915_gem_exec_object *exec_list = NULL; struct drm_gem_object **object_list = NULL; @@ -1724,6 +1709,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, */ seqno = i915_add_request(dev, flush_domains); BUG_ON(seqno == 0); + i915_file_priv->mm.last_gem_seqno = seqno; for (i = 0; i < args->buffer_count; i++) { struct drm_gem_object *obj = object_list[i]; struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -1881,7 +1867,7 @@ int i915_gem_throttle_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { - return i915_gem_ring_throttle(dev); + return i915_gem_ring_throttle(dev, file_priv); } int i915_gem_init_object(struct drm_gem_object *obj) -- cgit v1.2.3 From baf521369478eff2842b99feda16f9d145402d27 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 14:28:18 -0700 Subject: [intel-gem] Pin objects during execbuffer Pinning the objects avoids accidentally evicting them while binding other objects. --- linux-core/i915_gem.c | 76 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 24 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index c2d1fab6..d14b2f2d 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1298,27 +1298,25 @@ i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle) #endif /** - * Bind an object to the GTT and evaluate the relocations landing in it. + * Pin an object to the GTT and evaluate the relocations landing in it. */ static int -i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, - struct drm_file *file_priv, - struct drm_i915_gem_exec_object *entry) +i915_gem_object_pin_and_relocate(struct drm_gem_object *obj, + struct drm_file *file_priv, + struct drm_i915_gem_exec_object *entry) { struct drm_device *dev = obj->dev; struct drm_i915_gem_relocation_entry reloc; struct drm_i915_gem_relocation_entry __user *relocs; struct drm_i915_gem_object *obj_priv = obj->driver_private; - int i; + int i, ret; uint32_t last_reloc_offset = -1; void *reloc_page = NULL; /* Choose the GTT offset for our buffer and put it there. */ - if (obj_priv->gtt_space == NULL) { - i915_gem_object_bind_to_gtt(obj, (unsigned) entry->alignment); - if (obj_priv->gtt_space == NULL) - return -ENOMEM; - } + ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment); + if (ret) + return ret; entry->offset = obj_priv->gtt_offset; @@ -1334,13 +1332,17 @@ i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, int ret; ret = copy_from_user(&reloc, relocs + i, sizeof(reloc)); - if (ret != 0) + if (ret != 0) { + i915_gem_object_unpin(obj); return ret; + } target_obj = drm_gem_object_lookup(obj->dev, file_priv, reloc.target_handle); - if (target_obj == NULL) + if (target_obj == NULL) { + i915_gem_object_unpin(obj); return -EINVAL; + } target_obj_priv = target_obj->driver_private; /* The target buffer should have appeared before us in the @@ -1350,6 +1352,7 @@ i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, DRM_ERROR("No GTT space found for object %d\n", reloc.target_handle); drm_gem_object_unreference(target_obj); + i915_gem_object_unpin(obj); return -EINVAL; } @@ -1359,6 +1362,7 @@ i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, obj, reloc.target_handle, (int) reloc.offset, (int) obj->size); drm_gem_object_unreference(target_obj); + i915_gem_object_unpin(obj); return -EINVAL; } if (reloc.offset & 3) { @@ -1367,6 +1371,7 @@ i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, obj, reloc.target_handle, (int) reloc.offset); drm_gem_object_unreference(target_obj); + i915_gem_object_unpin(obj); return -EINVAL; } @@ -1380,6 +1385,7 @@ i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, reloc.write_domain, target_obj->pending_write_domain); drm_gem_object_unreference(target_obj); + i915_gem_object_unpin(obj); return -EINVAL; } @@ -1440,6 +1446,7 @@ i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, last_reloc_offset = reloc_offset; if (reloc_page == NULL) { drm_gem_object_unreference(target_obj); + i915_gem_object_unpin(obj); return -ENOMEM; } } @@ -1462,6 +1469,7 @@ i915_gem_object_bind_and_relocate(struct drm_gem_object *obj, ret = copy_to_user(relocs + i, &reloc, sizeof(reloc)); if (ret != 0) { drm_gem_object_unreference(target_obj); + i915_gem_object_unpin(obj); return ret; } @@ -1573,7 +1581,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_i915_gem_exec_object *exec_list = NULL; struct drm_gem_object **object_list = NULL; struct drm_gem_object *batch_obj; - int ret, i; + int ret, i, pinned = 0; uint64_t exec_offset; uint32_t seqno, flush_domains; @@ -1632,13 +1640,14 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, object_list[i]->pending_read_domains = 0; object_list[i]->pending_write_domain = 0; - ret = i915_gem_object_bind_and_relocate(object_list[i], - file_priv, - &exec_list[i]); + ret = i915_gem_object_pin_and_relocate(object_list[i], + file_priv, + &exec_list[i]); if (ret) { DRM_ERROR("object bind and relocate failed %d\n", ret); goto err; } + pinned = i; } /* Set the pending read domains for the batch buffer to COMMAND */ @@ -1735,6 +1744,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, args->buffer_count, ret); err: if (object_list != NULL) { + for (i = 0; i < pinned; i++) + i915_gem_object_unpin (object_list[i]); for (i = 0; i < args->buffer_count; i++) drm_gem_object_unreference(object_list[i]); } @@ -1752,32 +1763,47 @@ pre_mutex_err: int i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) { - struct drm_device *dev = obj->dev; struct drm_i915_gem_object *obj_priv = obj->driver_private; int ret; if (obj_priv->gtt_space == NULL) { ret = i915_gem_object_bind_to_gtt(obj, alignment); if (ret != 0) { - DRM_ERROR("Failure to bind in " - "i915_gem_pin_ioctl(): %d\n", - ret); - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); + DRM_ERROR("Failure to bind: %d", ret); return ret; } } - obj_priv->pin_count++; + + /* If the object is not active and not pending a flush, + * remove it from the inactive list + */ + if (obj_priv->pin_count == 1 && + !obj_priv->active && + obj->write_domain == 0) + list_del_init(&obj_priv->list); + return 0; } void i915_gem_object_unpin(struct drm_gem_object *obj) { + struct drm_device *dev = obj->dev; + drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; obj_priv->pin_count--; + BUG_ON(obj_priv->pin_count < 0); + BUG_ON(obj_priv->gtt_space == NULL); + + /* If the object is no longer pinned, and is + * neither active nor being flushed, then stick it on + * the inactive list + */ + if (obj_priv->pin_count == 0 && + !obj_priv->active && obj->write_domain == 0) + list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list); } int @@ -1963,8 +1989,10 @@ i915_gem_init_ringbuffer(struct drm_device *dev) obj_priv = obj->driver_private; ret = i915_gem_object_pin(obj, 4096); - if (ret != 0) + if (ret != 0) { + drm_gem_object_unreference(obj); return ret; + } /* Set up the kernel mapping for the ring. */ dev_priv->ring.Size = obj->size; -- cgit v1.2.3 From 4086cdb6550a4e957fd436c77a6260204e026538 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 15:38:13 -0700 Subject: [intel-gem] Left the last exec buffer pinned. oops. Loop end variable 'pinned' was set one too low. --- linux-core/i915_gem.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index d14b2f2d..210ae9d9 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1647,7 +1647,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, DRM_ERROR("object bind and relocate failed %d\n", ret); goto err; } - pinned = i; + pinned = i + 1; } /* Set the pending read domains for the batch buffer to COMMAND */ @@ -1745,7 +1745,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, err: if (object_list != NULL) { for (i = 0; i < pinned; i++) - i915_gem_object_unpin (object_list[i]); + i915_gem_object_unpin(object_list[i]); + for (i = 0; i < args->buffer_count; i++) drm_gem_object_unreference(object_list[i]); } -- cgit v1.2.3 From 217beb9c8de01417ac6219b54bd25046da6d4c7a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 15:43:02 -0700 Subject: [intel-gem] add gtt and pin counts to /proc/dri/*/gem_objects Not quite portable, but these are useful for intel. Some more general mechanism could be done... --- linux-core/drmP.h | 5 +++++ linux-core/drm_gem.c | 7 +++++++ linux-core/drm_proc.c | 4 ++++ linux-core/i915_gem.c | 26 +++++++++++++++++++------- 4 files changed, 35 insertions(+), 7 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 02826c8d..ba25f3a8 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -965,6 +965,11 @@ struct drm_device { spinlock_t object_name_lock; struct idr object_name_idr; atomic_t object_count; + atomic_t object_memory; + atomic_t pin_count; + atomic_t pin_memory; + atomic_t gtt_count; + atomic_t gtt_memory; uint32_t invalidate_domains; /* domains pending invalidation */ uint32_t flush_domains; /* domains pending flush */ /*@} */ diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c index 47b55434..434155b3 100644 --- a/linux-core/drm_gem.c +++ b/linux-core/drm_gem.c @@ -74,6 +74,11 @@ drm_gem_init(struct drm_device *dev) spin_lock_init(&dev->object_name_lock); idr_init(&dev->object_name_idr); atomic_set(&dev->object_count, 0); + atomic_set(&dev->object_memory, 0); + atomic_set(&dev->pin_count, 0); + atomic_set(&dev->pin_memory, 0); + atomic_set(&dev->gtt_count, 0); + atomic_set(&dev->gtt_memory, 0); return 0; } @@ -106,6 +111,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size) return NULL; } atomic_inc(&dev->object_count); + atomic_add(obj->size, &dev->object_memory); return obj; } EXPORT_SYMBOL(drm_gem_object_alloc); @@ -376,6 +382,7 @@ drm_gem_object_free(struct kref *kref) fput(obj->filp); atomic_dec(&dev->object_count); + atomic_sub(obj->size, &dev->object_memory); kfree(obj); } EXPORT_SYMBOL(drm_gem_object_free); diff --git a/linux-core/drm_proc.c b/linux-core/drm_proc.c index b1b976b2..71d55274 100644 --- a/linux-core/drm_proc.c +++ b/linux-core/drm_proc.c @@ -655,6 +655,10 @@ static int drm_gem_object_info(char *buf, char **start, off_t offset, *start = &buf[offset]; *eof = 0; DRM_PROC_PRINT ("%d objects\n", atomic_read (&dev->object_count)); + DRM_PROC_PRINT ("%d object bytes\n", atomic_read (&dev->object_memory)); + DRM_PROC_PRINT ("%d pinned\n", atomic_read (&dev->pin_count)); + DRM_PROC_PRINT ("%d pin bytes\n", atomic_read (&dev->pin_memory)); + DRM_PROC_PRINT ("%d gtt bytes\n", atomic_read (&dev->gtt_memory)); if (len > request + offset) return request; *eof = 1; diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 210ae9d9..cb5d663e 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -677,6 +677,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) static int i915_gem_object_unbind(struct drm_gem_object *obj) { + struct drm_device *dev = obj->dev; struct drm_i915_gem_object *obj_priv = obj->driver_private; int ret = 0; @@ -706,6 +707,9 @@ i915_gem_object_unbind(struct drm_gem_object *obj) i915_gem_object_free_page_list(obj); + atomic_dec(&dev->gtt_count); + atomic_sub(obj->size, &dev->gtt_memory); + drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; @@ -995,6 +999,8 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) obj_priv->gtt_space = NULL; return -ENOMEM; } + atomic_inc(&dev->gtt_count); + atomic_add(obj->size, &dev->gtt_memory); /* Assert that the object is not currently in any GPU domain. As it * wasn't in the GTT, there shouldn't be any way it could have been in @@ -1764,6 +1770,7 @@ pre_mutex_err: int i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) { + struct drm_device *dev = obj->dev; struct drm_i915_gem_object *obj_priv = obj->driver_private; int ret; @@ -1779,10 +1786,12 @@ i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) /* If the object is not active and not pending a flush, * remove it from the inactive list */ - if (obj_priv->pin_count == 1 && - !obj_priv->active && - obj->write_domain == 0) - list_del_init(&obj_priv->list); + if (obj_priv->pin_count == 1) { + atomic_inc(&dev->pin_count); + atomic_add(obj->size, &dev->pin_memory); + if (!obj_priv->active && obj->write_domain == 0) + list_del_init(&obj_priv->list); + } return 0; } @@ -1802,9 +1811,12 @@ i915_gem_object_unpin(struct drm_gem_object *obj) * neither active nor being flushed, then stick it on * the inactive list */ - if (obj_priv->pin_count == 0 && - !obj_priv->active && obj->write_domain == 0) - list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list); + if (obj_priv->pin_count == 0) { + if (!obj_priv->active && obj->write_domain == 0) + list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list); + atomic_dec(&dev->pin_count); + atomic_sub(obj->size, &dev->pin_memory); + } } int -- cgit v1.2.3 From 73bc18cad8d1c6b4481a199cebf7f0a28d19c2bb Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 17:06:35 -0700 Subject: [intel-gem] Wait for rendering to complete before unbinding. Moving to the CPU domain doesn't ensure that rendering is finished, the buffer may still be in use as a texture or other data source. --- linux-core/i915_gem.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index cb5d663e..0f037acf 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -688,6 +688,19 @@ i915_gem_object_unbind(struct drm_gem_object *obj) if (obj_priv->gtt_space == NULL) return 0; + if (obj_priv->pin_count != 0) { + DRM_ERROR("Attempting to unbind pinned buffer\n"); + return -EINVAL; + } + + /* Wait for any rendering to complete + */ + ret = i915_gem_object_wait_rendering(obj); + if (ret) { + DRM_ERROR ("wait_rendering failed: %d\n", ret); + return ret; + } + /* Move the object to the CPU domain to ensure that * any possible CPU writes while it's not in the GTT * are flushed when we go to remap it. This will -- cgit v1.2.3 From a7139cb8511a9d31d9f79bcaae62020d30e09f90 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 19:35:22 -0700 Subject: [intel-gem] show total GTT space in /proc/dri/*/gem_objects --- linux-core/drmP.h | 1 + linux-core/drm_proc.c | 11 ++++++----- linux-core/i915_gem.c | 2 ++ 3 files changed, 9 insertions(+), 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index ba25f3a8..392d2ace 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -970,6 +970,7 @@ struct drm_device { atomic_t pin_memory; atomic_t gtt_count; atomic_t gtt_memory; + uint32_t gtt_total; uint32_t invalidate_domains; /* domains pending invalidation */ uint32_t flush_domains; /* domains pending flush */ /*@} */ diff --git a/linux-core/drm_proc.c b/linux-core/drm_proc.c index 71d55274..2bbe7eea 100644 --- a/linux-core/drm_proc.c +++ b/linux-core/drm_proc.c @@ -654,11 +654,12 @@ static int drm_gem_object_info(char *buf, char **start, off_t offset, *start = &buf[offset]; *eof = 0; - DRM_PROC_PRINT ("%d objects\n", atomic_read (&dev->object_count)); - DRM_PROC_PRINT ("%d object bytes\n", atomic_read (&dev->object_memory)); - DRM_PROC_PRINT ("%d pinned\n", atomic_read (&dev->pin_count)); - DRM_PROC_PRINT ("%d pin bytes\n", atomic_read (&dev->pin_memory)); - DRM_PROC_PRINT ("%d gtt bytes\n", atomic_read (&dev->gtt_memory)); + DRM_PROC_PRINT("%d objects\n", atomic_read (&dev->object_count)); + DRM_PROC_PRINT("%d object bytes\n", atomic_read (&dev->object_memory)); + DRM_PROC_PRINT("%d pinned\n", atomic_read (&dev->pin_count)); + DRM_PROC_PRINT("%d pin bytes\n", atomic_read (&dev->pin_memory)); + DRM_PROC_PRINT("%d gtt bytes\n", atomic_read (&dev->gtt_memory)); + DRM_PROC_PRINT("%d gtt total\n", dev->gtt_total); if (len > request + offset) return request; *eof = 1; diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 0f037acf..e07cf1b9 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -65,6 +65,8 @@ i915_gem_init_ioctl(struct drm_device *dev, void *data, drm_memrange_init(&dev_priv->mm.gtt_space, args->gtt_start, args->gtt_end - args->gtt_start); + dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start); + mutex_unlock(&dev->struct_mutex); return 0; -- cgit v1.2.3 From 732b1960742042eb33f49c2b3cdd2d36eadbc920 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 19:37:44 -0700 Subject: [intel-gem] whitespace fixes --- linux-core/i915_gem.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index e07cf1b9..78d0d952 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -66,7 +66,7 @@ i915_gem_init_ioctl(struct drm_device *dev, void *data, args->gtt_end - args->gtt_start); dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start); - + mutex_unlock(&dev->struct_mutex); return 0; @@ -500,7 +500,7 @@ i915_gem_retire_requests(struct drm_device *dev) list_del(&request->list); drm_free(request, sizeof(*request), DRM_MEM_DRIVER); } else - break; + break; } } @@ -858,7 +858,7 @@ i915_gem_evict_something(struct drm_device *dev) list); ret = i915_wait_request(dev, request->seqno); - + /* if waiting caused an object to become inactive, * then loop around and wait for it. Otherwise, we * assume that waiting freed and unbound something, -- cgit v1.2.3 From 68856b619bc1a2e91e67764911c8af4e2466fad9 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 19:40:16 -0700 Subject: [intel-gem] Debugging -- verify inactive list invariants Inactive list elements may not be pinned, active or have non-CPU write domains. --- linux-core/i915_gem.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 78d0d952..8741eecd 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -35,6 +35,7 @@ #define WATCH_EXEC 0 #define WATCH_LRU 0 #define WATCH_RELOC 0 +#define WATCH_INACTIVE 0 static int i915_gem_object_set_domain(struct drm_gem_object *obj, @@ -303,6 +304,26 @@ i915_gem_object_move_to_active(struct drm_gem_object *obj) &dev_priv->mm.active_list); } +#if WATCH_INACTIVE +static void +i915_verify_inactive(struct drm_device *dev, char *file, int line) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + + list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { + obj = obj_priv->obj; + if (obj_priv->pin_count || obj_priv->active || (obj->write_domain & ~I915_GEM_DOMAIN_CPU)) + DRM_ERROR("inactive %p (p %d a %d w %x) %s:%d\n", + obj, + obj_priv->pin_count, obj_priv->active, obj->write_domain, file, line); + } +} +#else +#define i915_verify_inactive(dev,file,line) +#endif + static void i915_gem_object_move_to_inactive(struct drm_gem_object *obj) { @@ -310,6 +331,7 @@ i915_gem_object_move_to_inactive(struct drm_gem_object *obj) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; + i915_verify_inactive(dev, __FILE__, __LINE__); if (obj_priv->pin_count != 0) list_del_init(&obj_priv->list); else @@ -319,6 +341,7 @@ i915_gem_object_move_to_inactive(struct drm_gem_object *obj) obj_priv->active = 0; drm_gem_object_unreference(obj); } + i915_verify_inactive(dev, __FILE__, __LINE__); } /** @@ -1635,6 +1658,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); + i915_verify_inactive(dev, __FILE__, __LINE__); if (dev_priv->mm.suspended) { DRM_ERROR("Execbuf while VT-switched.\n"); mutex_unlock(&dev->struct_mutex); @@ -1676,6 +1700,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND; batch_obj->pending_write_domain = 0; + i915_verify_inactive(dev, __FILE__, __LINE__); + for (i = 0; i < args->buffer_count; i++) { struct drm_gem_object *obj = object_list[i]; struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -1698,9 +1724,13 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, goto err; } + i915_verify_inactive(dev, __FILE__, __LINE__); + /* Flush/invalidate caches and chipset buffer */ flush_domains = i915_gem_dev_set_domain(dev); + i915_verify_inactive(dev, __FILE__, __LINE__); + #if WATCH_COHERENCY for (i = 0; i < args->buffer_count; i++) { i915_gem_object_check_coherency(object_list[i], @@ -1730,6 +1760,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, */ flush_domains |= i915_retire_commands(dev); + i915_verify_inactive(dev, __FILE__, __LINE__); + /* * Get a seqno representing the execution of the current buffer, * which we can wait on. We would like to mitigate these interrupts, @@ -1754,6 +1786,8 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, i915_dump_lru(dev, __func__); #endif + i915_verify_inactive(dev, __FILE__, __LINE__); + /* Copy the new buffer offsets back to the user's exec list. */ ret = copy_to_user((struct drm_i915_relocation_entry __user *) (uintptr_t) args->buffers_ptr, @@ -1789,6 +1823,7 @@ i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) struct drm_i915_gem_object *obj_priv = obj->driver_private; int ret; + i915_verify_inactive(dev, __FILE__, __LINE__); if (obj_priv->gtt_space == NULL) { ret = i915_gem_object_bind_to_gtt(obj, alignment); if (ret != 0) { @@ -1807,6 +1842,7 @@ i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) if (!obj_priv->active && obj->write_domain == 0) list_del_init(&obj_priv->list); } + i915_verify_inactive(dev, __FILE__, __LINE__); return 0; } @@ -1818,10 +1854,11 @@ i915_gem_object_unpin(struct drm_gem_object *obj) drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; + i915_verify_inactive(dev, __FILE__, __LINE__); obj_priv->pin_count--; BUG_ON(obj_priv->pin_count < 0); BUG_ON(obj_priv->gtt_space == NULL); - + /* If the object is no longer pinned, and is * neither active nor being flushed, then stick it on * the inactive list @@ -1832,6 +1869,7 @@ i915_gem_object_unpin(struct drm_gem_object *obj) atomic_dec(&dev->pin_count); atomic_sub(obj->size, &dev->pin_memory); } + i915_verify_inactive(dev, __FILE__, __LINE__); } int -- cgit v1.2.3 From 93c2871eccc1abde0d88ea439cf963c4895a26fc Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 19:43:40 -0700 Subject: [intel-gem] BUG_ON active objects in gem_object_unbind Now that gem_object_unbind waits for rendering to complete, objects should not be active when they are being pulled from the GTT. BUG_ON if this is broken. --- linux-core/i915_gem.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 8741eecd..a15d7f23 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -734,8 +734,10 @@ i915_gem_object_unbind(struct drm_gem_object *obj) */ ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU); - if (ret) + if (ret) { + DRM_ERROR("set_domain failed: %d\n", ret); return ret; + } if (obj_priv->agp_mem != NULL) { drm_unbind_agp(obj_priv->agp_mem); @@ -743,6 +745,8 @@ i915_gem_object_unbind(struct drm_gem_object *obj) obj_priv->agp_mem = NULL; } + BUG_ON(obj_priv->active); + i915_gem_object_free_page_list(obj); atomic_dec(&dev->gtt_count); @@ -752,15 +756,9 @@ i915_gem_object_unbind(struct drm_gem_object *obj) obj_priv->gtt_space = NULL; /* Remove ourselves from the LRU list if present. */ - if (!list_empty(&obj_priv->list)) { + if (!list_empty(&obj_priv->list)) list_del_init(&obj_priv->list); - if (obj_priv->active) { - DRM_ERROR("Failed to wait on buffer when unbinding, " - "continued anyway.\n"); - obj_priv->active = 0; - drm_gem_object_unreference(obj); - } - } + return 0; } -- cgit v1.2.3 From 19c3418848ccdbb163cd16b354b14b0559813d6c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 19:47:23 -0700 Subject: [intel-gem] inactive list may contain objects in CPU write domain Pin/unpin need to know whether to remove/add objects from the inactive list, inactive objects cannot be in any GPU write domain as those would be on the flushing list instead. However, inactive objects may be in the CPU write domain. --- linux-core/i915_gem.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index a15d7f23..5e5c0ea1 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1837,7 +1837,8 @@ i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) if (obj_priv->pin_count == 1) { atomic_inc(&dev->pin_count); atomic_add(obj->size, &dev->pin_memory); - if (!obj_priv->active && obj->write_domain == 0) + if (!obj_priv->active && (obj->write_domain & ~I915_GEM_DOMAIN_CPU) == 0 && + !list_empty(&obj_priv->list)) list_del_init(&obj_priv->list); } i915_verify_inactive(dev, __FILE__, __LINE__); @@ -1862,8 +1863,9 @@ i915_gem_object_unpin(struct drm_gem_object *obj) * the inactive list */ if (obj_priv->pin_count == 0) { - if (!obj_priv->active && obj->write_domain == 0) - list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list); + if (!obj_priv->active && (obj->write_domain & ~I915_GEM_DOMAIN_CPU) == 0) + list_move_tail(&obj_priv->list, + &dev_priv->mm.inactive_list); atomic_dec(&dev->pin_count); atomic_sub(obj->size, &dev->pin_memory); } -- cgit v1.2.3 From 3e48e144992fb11b31875989d45bc8a7c041cdef Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2008 19:49:47 -0700 Subject: [intel-gem] Execute MI_FLUSH in leavevt_ioctl In leavevt_ioctl, queue an MI_FLUSH and then block waiting for it to complete. This will empty the active and flushing lists. That leaves only the inactive list to evict. --- linux-core/i915_gem.c | 52 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 5e5c0ea1..308674d8 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -2127,6 +2127,10 @@ i915_gem_entervt_ioctl(struct drm_device *dev, void *data, return ret; mutex_lock(&dev->struct_mutex); + BUG_ON(!list_empty(&dev_priv->mm.active_list)); + BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); + BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); + BUG_ON(!list_empty(&dev_priv->mm.request_list)); dev_priv->mm.suspended = 0; mutex_unlock(&dev->struct_mutex); return 0; @@ -2170,6 +2174,8 @@ i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { drm_i915_private_t *dev_priv = dev->dev_private; + uint32_t seqno; + int ret; mutex_lock(&dev->struct_mutex); /* Hack! Don't let anybody do execbuf while we don't control the chip. @@ -2177,32 +2183,40 @@ i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, */ dev_priv->mm.suspended = 1; - /* Move all buffers out of the GTT. */ - i915_gem_evict_from_list(dev, &dev_priv->mm.active_list); - i915_gem_evict_from_list(dev, &dev_priv->mm.flushing_list); - i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list); + i915_kernel_lost_context(dev); - /* Make sure the harware's idle. */ - while (!list_empty(&dev_priv->mm.request_list)) { - struct drm_i915_gem_request *request; - int ret; + /* Flush the GPU along with all non-CPU write domains + */ + i915_gem_flush(dev, ~I915_GEM_DOMAIN_CPU, ~I915_GEM_DOMAIN_CPU); + seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU); + if (seqno == 0) { + mutex_unlock(&dev->struct_mutex); + return -ENOMEM; + } + ret = i915_wait_request(dev, seqno); + if (ret) { + mutex_unlock(&dev->struct_mutex); + return ret; + } - request = list_first_entry(&dev_priv->mm.request_list, - struct drm_i915_gem_request, - list); + /* Active and flushing should now be empty as we've + * waited for a sequence higher than any pending execbuffer + */ + BUG_ON(!list_empty(&dev_priv->mm.active_list)); + BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); - ret = i915_wait_request(dev, request->seqno); - if (ret != 0) { - DRM_ERROR("Error waiting for idle at LeaveVT: %d\n", - ret); - mutex_unlock(&dev->struct_mutex); - return ret; - } - } + /* Request should now be empty as we've also waited + * for the last request in the list + */ + BUG_ON(!list_empty(&dev_priv->mm.request_list)); + + /* Move all buffers out of the GTT. */ + i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list); BUG_ON(!list_empty(&dev_priv->mm.active_list)); BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); + BUG_ON(!list_empty(&dev_priv->mm.request_list)); i915_gem_cleanup_ringbuffer(dev); -- cgit v1.2.3 From c847271179da382af52a6874e2edec586b88af75 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 16 Jun 2008 13:09:31 -0700 Subject: [linux] Use the device's irq for handler setup instead of stale dev->irq. This fixes registration when MSI is set up after the stub function fills in dev->irq. Otherwise /proc/interrupts would report attachment to the fasteoi interrupt. dev->irq is still exposed (and updated at IRQ setup) for the drivers that use it for whatever reason. --- linux-core/drm_irq.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_irq.c b/linux-core/drm_irq.c index 318d9d7a..961f90c4 100644 --- a/linux-core/drm_irq.c +++ b/linux-core/drm_irq.c @@ -63,7 +63,7 @@ int drm_irq_by_busid(struct drm_device *dev, void *data, p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn)) return -EINVAL; - p->irq = dev->irq; + p->irq = dev->pdev->irq; DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum, p->irq); @@ -200,7 +200,7 @@ int drm_irq_install(struct drm_device * dev) if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) return -EINVAL; - if (dev->irq == 0) + if (dev->pdev->irq == 0) return -EINVAL; mutex_lock(&dev->struct_mutex); @@ -218,7 +218,7 @@ int drm_irq_install(struct drm_device * dev) dev->irq_enabled = 1; mutex_unlock(&dev->struct_mutex); - DRM_DEBUG("irq=%d\n", dev->irq); + DRM_DEBUG("irq=%d\n", dev->pdev->irq); /* Before installing handler */ dev->driver->irq_preinstall(dev); @@ -227,7 +227,7 @@ int drm_irq_install(struct drm_device * dev) if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED)) sh_flags = IRQF_SHARED; - ret = request_irq(dev->irq, dev->driver->irq_handler, + ret = request_irq(dev->pdev->irq, dev->driver->irq_handler, sh_flags, dev->devname, dev); if (ret < 0) { mutex_lock(&dev->struct_mutex); @@ -235,6 +235,10 @@ int drm_irq_install(struct drm_device * dev) mutex_unlock(&dev->struct_mutex); return ret; } + /* Expose the device irq to device drivers that want to export it for + * whatever reason. + */ + dev->irq = dev->pdev->irq; /* After installing handler */ ret = dev->driver->irq_postinstall(dev); @@ -270,11 +274,11 @@ int drm_irq_uninstall(struct drm_device * dev) if (!irq_enabled) return -EINVAL; - DRM_DEBUG("irq=%d\n", dev->irq); + DRM_DEBUG("irq=%d\n", dev->pdev->irq); dev->driver->irq_uninstall(dev); - free_irq(dev->irq, dev); + free_irq(dev->pdev->irq, dev); drm_vblank_cleanup(dev); @@ -308,7 +312,7 @@ int drm_control(struct drm_device *dev, void *data, if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) return 0; if (dev->if_version < DRM_IF_VERSION(1, 2) && - ctl->irq != dev->irq) + ctl->irq != dev->pdev->irq) return -EINVAL; return drm_irq_install(dev); case DRM_UNINST_HANDLER: @@ -489,7 +493,7 @@ int drm_wait_vblank(struct drm_device *dev, void *data, int ret = 0; unsigned int flags, seq, crtc; - if ((!dev->irq) || (!dev->irq_enabled)) + if ((!dev->pdev->irq) || (!dev->irq_enabled)) return -EINVAL; if (vblwait->request.type & -- cgit v1.2.3 From e7424e4580159b0ac3e232674dff5c862e851dff Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 16 Jun 2008 15:15:02 -0700 Subject: [intel] Quirk away MSI support on 945G/GM. The PCI caps register reports MSI support even though it isn't really there. --- linux-core/i915_drv.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index 012ca822..e399f374 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -628,7 +628,16 @@ static int probe(struct pci_dev *pdev, const struct pci_device_id *ent) { int ret; - (void) pci_enable_msi(pdev); + /* On the 945G/GM, the chipset reports the MSI capability on the + * integrated graphics even though the support isn't actually there + * according to the published specs. It doesn't appear to function + * correctly in testing on 945G. + * This may be a side effect of MSI having been made available for PEG + * and the registers being closely associated. + */ + if (pdev->device != 0x2772 && pdev->device != 0x27A2) + (void )pci_enable_msi(pdev); + ret = drm_get_dev(pdev, ent, &driver); if (ret && pdev->msi_enabled) pci_disable_msi(pdev); -- cgit v1.2.3 From 52e5d24fae4af6f2f4a5304a516c8c5ab347a11b Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 20 Jun 2008 00:19:42 -0700 Subject: [intel-gem] Add DRM_IOCTL_I915_GEM_SW_FINISH to flag CPU writes When a software fallback has completed, usermode must notify the kernel so that any scanout buffers can be synchronized. This ioctl should be called whenever a fallback completes to flush CPU and chipset caches. --- linux-core/i915_gem.c | 245 +++++++++++++++++++++++++++++++------------------- 1 file changed, 154 insertions(+), 91 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 308674d8..bd4aeaa7 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -47,6 +47,9 @@ i915_gem_set_domain(struct drm_gem_object *obj, uint32_t read_domains, uint32_t write_domain); +static void +i915_gem_clflush_object(struct drm_gem_object *obj); + int i915_gem_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -225,6 +228,45 @@ i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, return ret; } +/** + * Called when user space has done writes to this buffer + */ +int +i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_sw_finish *args = data; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + int ret = 0; + + if (!(dev->driver->driver_features & DRIVER_GEM)) + return -ENODEV; + + mutex_lock(&dev->struct_mutex); + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) { + mutex_unlock(&dev->struct_mutex); + return -EINVAL; + } + +#if WATCH_BUF + DRM_INFO("%s: sw_finish %d (%p)\n", + __func__, args->handle, obj); +#endif + obj_priv = obj->driver_private; + + /** Pinned buffers may be scanout, so flush the cache + */ + if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) { + i915_gem_clflush_object(obj); + drm_agp_chipset_flush(dev); + } + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + return ret; +} + /** * Maps the contents of an object, returning the address it is mapped * into. @@ -1180,13 +1222,16 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, uint32_t write_domain) { struct drm_device *dev = obj->dev; + struct drm_i915_gem_object *obj_priv = obj->driver_private; uint32_t invalidate_domains = 0; uint32_t flush_domains = 0; int ret; #if WATCH_BUF - DRM_INFO("%s: object %p read %08x write %08x\n", - __func__, obj, read_domains, write_domain); + DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n", + __func__, obj, + obj->read_domains, read_domains, + obj->write_domain, write_domain); #endif /* * If the object isn't moving to a new write domain, @@ -1234,6 +1279,12 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, obj->read_domains = read_domains; dev->invalidate_domains |= invalidate_domains; dev->flush_domains |= flush_domains; +#if WATCH_BUF + DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n", + __func__, + obj->read_domains, obj->write_domain, + dev->invalidate_domains, dev->flush_domains); +#endif return 0; } @@ -1899,6 +1950,14 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, return ret; } + /** XXX - flush the CPU caches for pinned objects + * as the X server doesn't manage domains yet + */ + if (obj->write_domain & I915_GEM_DOMAIN_CPU) { + i915_gem_clflush_object(obj); + drm_agp_chipset_flush(dev); + obj->write_domain = 0; + } args->offset = obj_priv->gtt_offset; drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); @@ -2000,43 +2059,105 @@ i915_gem_set_domain(struct drm_gem_object *obj, { struct drm_device *dev = obj->dev; int ret; + uint32_t flush_domains; BUG_ON(!mutex_is_locked(&dev->struct_mutex)); ret = i915_gem_object_set_domain(obj, read_domains, write_domain); if (ret) return ret; - i915_gem_dev_set_domain(obj->dev); + flush_domains = i915_gem_dev_set_domain(obj->dev); + + if (flush_domains & ~I915_GEM_DOMAIN_CPU) + (void) i915_add_request(dev, flush_domains); return 0; } -void -i915_gem_lastclose(struct drm_device *dev) +/** Unbinds all objects that are on the given buffer list. */ +static int +i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head) +{ + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + int ret; + + while (!list_empty(head)) { + obj_priv = list_first_entry(head, + struct drm_i915_gem_object, + list); + obj = obj_priv->obj; + + if (obj_priv->pin_count != 0) { + DRM_ERROR("Pinned object in unbind list\n"); + mutex_unlock(&dev->struct_mutex); + return -EINVAL; + } + + ret = i915_gem_object_unbind(obj); + if (ret != 0) { + DRM_ERROR("Error unbinding object in LeaveVT: %d\n", + ret); + mutex_unlock(&dev->struct_mutex); + return ret; + } + } + + + return 0; +} + +static int +i915_gem_idle(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; + uint32_t seqno; + int ret; - mutex_lock(&dev->struct_mutex); + if (dev_priv->mm.suspended) + return 0; - /* Assume that the chip has been idled at this point. Just pull them - * off the execution list and unref them. Since this is the last - * close, this is also the last ref and they'll go away. + /* Hack! Don't let anybody do execbuf while we don't control the chip. + * We need to replace this with a semaphore, or something. */ + dev_priv->mm.suspended = 1; - while (!list_empty(&dev_priv->mm.active_list)) { - struct drm_i915_gem_object *obj_priv; + i915_kernel_lost_context(dev); - obj_priv = list_first_entry(&dev_priv->mm.active_list, - struct drm_i915_gem_object, - list); + /* Flush the GPU along with all non-CPU write domains + */ + i915_gem_flush(dev, ~I915_GEM_DOMAIN_CPU, ~I915_GEM_DOMAIN_CPU); + seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU); - list_del_init(&obj_priv->list); - obj_priv->active = 0; - obj_priv->obj->write_domain = 0; - drm_gem_object_unreference(obj_priv->obj); + if (seqno == 0) { + mutex_unlock(&dev->struct_mutex); + return -ENOMEM; + } + ret = i915_wait_request(dev, seqno); + if (ret) { + mutex_unlock(&dev->struct_mutex); + return ret; } - mutex_unlock(&dev->struct_mutex); + /* Active and flushing should now be empty as we've + * waited for a sequence higher than any pending execbuffer + */ + BUG_ON(!list_empty(&dev_priv->mm.active_list)); + BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); + + /* Request should now be empty as we've also waited + * for the last request in the list + */ + BUG_ON(!list_empty(&dev_priv->mm.request_list)); + + /* Move all buffers out of the GTT. */ + i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list); + + BUG_ON(!list_empty(&dev_priv->mm.active_list)); + BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); + BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); + BUG_ON(!list_empty(&dev_priv->mm.request_list)); + return 0; } static int @@ -2136,91 +2257,33 @@ i915_gem_entervt_ioctl(struct drm_device *dev, void *data, return 0; } -/** Unbinds all objects that are on the given buffer list. */ -static int -i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head) +int +i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) { - struct drm_gem_object *obj; - struct drm_i915_gem_object *obj_priv; int ret; - while (!list_empty(head)) { - obj_priv = list_first_entry(head, - struct drm_i915_gem_object, - list); - obj = obj_priv->obj; - - if (obj_priv->pin_count != 0) { - DRM_ERROR("Pinned object in unbind list\n"); - mutex_unlock(&dev->struct_mutex); - return -EINVAL; - } - - ret = i915_gem_object_unbind(obj); - if (ret != 0) { - DRM_ERROR("Error unbinding object in LeaveVT: %d\n", - ret); - mutex_unlock(&dev->struct_mutex); - return ret; - } - } - + mutex_lock(&dev->struct_mutex); + ret = i915_gem_idle(dev); + if (ret == 0) + i915_gem_cleanup_ringbuffer(dev); + mutex_unlock(&dev->struct_mutex); return 0; } -int -i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) +void +i915_gem_lastclose(struct drm_device *dev) { - drm_i915_private_t *dev_priv = dev->dev_private; - uint32_t seqno; int ret; mutex_lock(&dev->struct_mutex); - /* Hack! Don't let anybody do execbuf while we don't control the chip. - * We need to replace this with a semaphore, or something. - */ - dev_priv->mm.suspended = 1; - - i915_kernel_lost_context(dev); - - /* Flush the GPU along with all non-CPU write domains - */ - i915_gem_flush(dev, ~I915_GEM_DOMAIN_CPU, ~I915_GEM_DOMAIN_CPU); - seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU); - if (seqno == 0) { - mutex_unlock(&dev->struct_mutex); - return -ENOMEM; - } - ret = i915_wait_request(dev, seqno); - if (ret) { - mutex_unlock(&dev->struct_mutex); - return ret; - } - /* Active and flushing should now be empty as we've - * waited for a sequence higher than any pending execbuffer - */ - BUG_ON(!list_empty(&dev_priv->mm.active_list)); - BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); - - /* Request should now be empty as we've also waited - * for the last request in the list - */ - BUG_ON(!list_empty(&dev_priv->mm.request_list)); - - /* Move all buffers out of the GTT. */ - i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list); - - BUG_ON(!list_empty(&dev_priv->mm.active_list)); - BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); - BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); - BUG_ON(!list_empty(&dev_priv->mm.request_list)); + ret = i915_gem_idle(dev); + if (ret) + DRM_ERROR("failed to idle hardware: %d\n", ret); i915_gem_cleanup_ringbuffer(dev); - + mutex_unlock(&dev->struct_mutex); - - return 0; } -- cgit v1.2.3 From 918420deefb978d4e572121b4273d717bdbfde8e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 20 Jun 2008 00:21:37 -0700 Subject: [intel-gem] Use shmem_getpage instead of find_or_create_page find_or_create_page doesn't quite set up pages correctly; any newly created pages aren't hooked into the shmem object quite right; user space mmaps of those pages end up mapping pages full of zeros which then get written to the real pages inappropriately. This patch requires that the kernel export shmem_getpage. --- linux-core/i915_gem.c | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index bd4aeaa7..129c9f3e 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -320,8 +320,13 @@ i915_gem_object_free_page_list(struct drm_gem_object *obj) for (i = 0; i < page_count; i++) - if (obj_priv->page_list[i] != NULL) + if (obj_priv->page_list[i] != NULL) { + if (obj_priv->dirty) + set_page_dirty(obj_priv->page_list[i]); + mark_page_accessed(obj_priv->page_list[i]); page_cache_release(obj_priv->page_list[i]); + } + obj_priv->dirty = 0; drm_free(obj_priv->page_list, page_count * sizeof(struct page *), @@ -969,6 +974,11 @@ i915_gem_object_get_page_list(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; int page_count, i; + struct address_space *mapping; + struct inode *inode; + struct page *page; + int ret; + if (obj_priv->page_list) return 0; @@ -984,16 +994,25 @@ i915_gem_object_get_page_list(struct drm_gem_object *obj) return -ENOMEM; } + inode = obj->filp->f_path.dentry->d_inode; + mapping = inode->i_mapping; for (i = 0; i < page_count; i++) { - obj_priv->page_list[i] = - find_or_create_page(obj->filp->f_mapping, i, GFP_HIGHUSER); - - if (obj_priv->page_list[i] == NULL) { - DRM_ERROR("Failed to find_or_create_page()\n"); - i915_gem_object_free_page_list(obj); - return -ENOMEM; + page = find_get_page(mapping, i); + if (page == NULL || !PageUptodate(page)) { + if (page) { + page_cache_release(page); + page = NULL; + } + ret = shmem_getpage(inode, i, &page, SGP_DIRTY, NULL); + + if (ret) { + DRM_ERROR("shmem_getpage failed: %d\n", ret); + i915_gem_object_free_page_list(obj); + return ret; + } + unlock_page(page); } - unlock_page(obj_priv->page_list[i]); + obj_priv->page_list[i] = page; } return 0; } @@ -1239,6 +1258,8 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, */ if (write_domain == 0) read_domains |= obj->read_domains; + else + obj_priv->dirty = 1; /* * Flush the current write domain if @@ -2046,6 +2067,11 @@ int i915_gem_init_object(struct drm_gem_object *obj) void i915_gem_free_object(struct drm_gem_object *obj) { + struct drm_i915_gem_object *obj_priv = obj->driver_private; + + while (obj_priv->pin_count > 0) + i915_gem_object_unpin(obj); + i915_gem_object_unbind(obj); drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); -- cgit v1.2.3 From 2bd9799e4cf0d778e46453422157143e36274062 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 20 Jun 2008 16:40:14 -0700 Subject: Add device-specific proc_init and proc_cleanup hooks This allows device drivers to add proc files --- linux-core/drmP.h | 5 ++++- linux-core/drm_drv.c | 2 +- linux-core/drm_stub.c | 22 ++++++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 392d2ace..ec8a61d4 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -770,6 +770,9 @@ struct drm_driver { void (*set_version) (struct drm_device *dev, struct drm_set_version *sv); + int (*proc_init)(struct drm_minor *minor); + void (*proc_cleanup)(struct drm_minor *minor); + /** * Driver-specific constructor for drm_gem_objects, to set up * obj->driver_private. @@ -1287,7 +1290,7 @@ extern void drm_agp_chipset_flush(struct drm_device *dev); extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent, struct drm_driver *driver); extern int drm_put_dev(struct drm_device *dev); -extern int drm_put_minor(struct drm_minor **minor); +extern int drm_put_minor(struct drm_device *dev); extern unsigned int drm_debug; /* 1 to enable debug output */ extern struct class *drm_class; diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index 980752c2..bc32ed50 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -422,7 +422,7 @@ static void drm_cleanup(struct drm_device * dev) drm_memrange_takedown(&dev->offset_manager); drm_ht_remove(&dev->object_hash); - drm_put_minor(&dev->primary); + drm_put_minor(dev); if (drm_put_dev(dev)) DRM_ERROR("Cannot unload module\n"); } diff --git a/linux-core/drm_stub.c b/linux-core/drm_stub.c index 55841826..1aacd4ff 100644 --- a/linux-core/drm_stub.c +++ b/linux-core/drm_stub.c @@ -222,6 +222,13 @@ static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int t DRM_ERROR("DRM: Failed to initialize /proc/dri.\n"); goto err_mem; } + if (dev->driver->proc_init) { + ret = dev->driver->proc_init(new_minor); + if (ret) { + DRM_ERROR("DRM: Driver failed to initialize /proc/dri.\n"); + goto err_mem; + } + } } else new_minor->dev_root = NULL; @@ -238,8 +245,11 @@ static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int t err_g2: - if (new_minor->type == DRM_MINOR_LEGACY) + if (new_minor->type == DRM_MINOR_LEGACY) { + if (dev->driver->proc_cleanup) + dev->driver->proc_cleanup(new_minor); drm_proc_cleanup(new_minor, drm_proc_root); + } err_mem: kfree(new_minor); err_idr: @@ -302,7 +312,7 @@ int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent, return 0; err_g4: - drm_put_minor(&dev->primary); + drm_put_minor(dev); err_g3: if (!drm_fb_loaded) pci_disable_device(pdev); @@ -358,13 +368,17 @@ int drm_put_dev(struct drm_device * dev) * last minor released. * */ -int drm_put_minor(struct drm_minor **minor_p) +int drm_put_minor(struct drm_device *dev) { + struct drm_minor **minor_p = &dev->primary; struct drm_minor *minor = *minor_p; DRM_DEBUG("release secondary minor %d\n", minor->index); - if (minor->type == DRM_MINOR_LEGACY) + if (minor->type == DRM_MINOR_LEGACY) { + if (dev->driver->proc_cleanup) + dev->driver->proc_cleanup(minor); drm_proc_cleanup(minor, drm_proc_root); + } drm_sysfs_device_remove(minor); idr_remove(&drm_minors_idr, minor->index); -- cgit v1.2.3 From 71b1623e22c54d42837840a1d0479127a5049caf Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 20 Jun 2008 21:07:46 -0700 Subject: [intel-gem] Add intel-specific /proc entries to help monitor gem operation This adds gem_active, gem_flushing, gem_inactive, gem_request and gem_seqno entries to monitor gem operation and help debug issues. --- linux-core/i915_drv.c | 2 + linux-core/i915_gem.c | 221 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 222 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_drv.c b/linux-core/i915_drv.c index e399f374..8eec336b 100644 --- a/linux-core/i915_drv.c +++ b/linux-core/i915_drv.c @@ -589,6 +589,8 @@ static struct drm_driver driver = { .reclaim_buffers = drm_core_reclaim_buffers, .get_map_ofs = drm_core_get_map_ofs, .get_reg_ofs = drm_core_get_reg_ofs, + .proc_init = i915_gem_proc_init, + .proc_cleanup = i915_gem_proc_cleanup, .ioctls = i915_ioctls, .gem_init_object = i915_gem_init_object, .gem_free_object = i915_gem_free_object, diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 129c9f3e..e086bc1b 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -536,7 +536,7 @@ i915_seqno_passed(uint32_t seq1, uint32_t seq2) return (int32_t)(seq1 - seq2) >= 0; } -static uint32_t +uint32_t i915_get_gem_seqno(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; @@ -604,11 +604,13 @@ i915_wait_request(struct drm_device *dev, uint32_t seqno) BUG_ON(seqno == 0); if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) { + dev_priv->mm.waiting_gem_seqno = seqno; i915_user_irq_on(dev_priv); ret = wait_event_interruptible(dev_priv->irq_queue, i915_seqno_passed(i915_get_gem_seqno(dev), seqno)); i915_user_irq_off(dev_priv); + dev_priv->mm.waiting_gem_seqno = 0; } if (ret) DRM_ERROR("%s returns %d (awaiting %d at %d)\n", @@ -2298,6 +2300,223 @@ i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, return 0; } +static int i915_gem_active_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Active:\n"); + list_for_each_entry(obj_priv, &dev_priv->mm.active_list, + list) + { + struct drm_gem_object *obj = obj_priv->obj; + if (obj->name) { + DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", + obj, obj->name, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } else { + DRM_PROC_PRINT(" %p: %08x %08x %d\n", + obj, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } + } + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static int i915_gem_flushing_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Flushing:\n"); + list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, + list) + { + struct drm_gem_object *obj = obj_priv->obj; + if (obj->name) { + DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", + obj, obj->name, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } else { + DRM_PROC_PRINT(" %p: %08x %08x %d\n", obj, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } + } + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static int i915_gem_inactive_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Inactive:\n"); + list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, + list) + { + struct drm_gem_object *obj = obj_priv->obj; + if (obj->name) { + DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", + obj, obj->name, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } else { + DRM_PROC_PRINT(" %p: %08x %08x %d\n", obj, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } + } + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static int i915_gem_request_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_request *gem_request; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Request:\n"); + list_for_each_entry(gem_request, &dev_priv->mm.request_list, + list) + { + DRM_PROC_PRINT (" %d @ %d %08x\n", + gem_request->seqno, + (int) (jiffies - gem_request->emitted_jiffies), + gem_request->flush_domains); + } + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static int i915_gem_seqno_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Current sequence: %d\n", i915_get_gem_seqno(dev)); + DRM_PROC_PRINT("Waiter sequence: %d\n", dev_priv->mm.waiting_gem_seqno); + DRM_PROC_PRINT("IRQ sequence: %d\n", dev_priv->mm.irq_gem_seqno); + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + + +static struct drm_proc_list { + const char *name; /**< file name */ + int (*f) (char *, char **, off_t, int, int *, void *); /**< proc callback*/ +} i915_gem_proc_list[] = { + {"gem_active", i915_gem_active_info}, + {"gem_flushing", i915_gem_flushing_info}, + {"gem_inactive", i915_gem_inactive_info}, + {"gem_request", i915_gem_request_info}, + {"gem_seqno", i915_gem_seqno_info}, +}; + +#define I915_GEM_PROC_ENTRIES ARRAY_SIZE(i915_gem_proc_list) + +int i915_gem_proc_init(struct drm_minor *minor) +{ + struct proc_dir_entry *ent; + int i, j; + + for (i = 0; i < I915_GEM_PROC_ENTRIES; i++) { + ent = create_proc_entry(i915_gem_proc_list[i].name, + S_IFREG | S_IRUGO, minor->dev_root); + if (!ent) { + DRM_ERROR("Cannot create /proc/dri/.../%s\n", + i915_gem_proc_list[i].name); + for (j = 0; j < i; j++) + remove_proc_entry(i915_gem_proc_list[i].name, + minor->dev_root); + return -1; + } + ent->read_proc = i915_gem_proc_list[i].f; + ent->data = minor; + } + return 0; +} + +void i915_gem_proc_cleanup(struct drm_minor *minor) +{ + int i; + + if (!minor->dev_root) + return; + + for (i = 0; i < I915_GEM_PROC_ENTRIES; i++) + remove_proc_entry(i915_gem_proc_list[i].name, minor->dev_root); +} + void i915_gem_lastclose(struct drm_device *dev) { -- cgit v1.2.3 From 54817317e9dd8a791418f97503fe574038dbe4b9 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 20 Jun 2008 21:10:42 -0700 Subject: [intel-gem] Use polling in i915_gem_idle instead of interrupts. While waiting for the hardware to idle on leavevt or lastclose, poll for the sync sequence number instead of waiting for an interrupt. This allows the code to bail if the hardware hangs for some reason. Also, this avoids issues with signals as the exisiting wait function is interruptible. --- linux-core/i915_gem.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index e086bc1b..27b6ddbb 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -2139,8 +2139,9 @@ static int i915_gem_idle(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; - uint32_t seqno; + uint32_t seqno, cur_seqno, last_seqno; int ret; + int stuck; if (dev_priv->mm.suspended) return 0; @@ -2161,11 +2162,26 @@ i915_gem_idle(struct drm_device *dev) mutex_unlock(&dev->struct_mutex); return -ENOMEM; } - ret = i915_wait_request(dev, seqno); - if (ret) { - mutex_unlock(&dev->struct_mutex); - return ret; + + dev_priv->mm.waiting_gem_seqno = seqno; + last_seqno = 0; + stuck = 0; + for (;;) { + cur_seqno = i915_get_gem_seqno(dev); + if (i915_seqno_passed(cur_seqno, seqno)) + break; + if (last_seqno == cur_seqno) { + if (stuck++ > 100) { + DRM_ERROR("hardware wedged\n"); + break; + } + } + msleep(10); + last_seqno = cur_seqno; } + dev_priv->mm.waiting_gem_seqno = 0; + + i915_gem_retire_requests(dev); /* Active and flushing should now be empty as we've * waited for a sequence higher than any pending execbuffer @@ -2521,14 +2537,17 @@ void i915_gem_lastclose(struct drm_device *dev) { int ret; + drm_i915_private_t *dev_priv = dev->dev_private; mutex_lock(&dev->struct_mutex); - ret = i915_gem_idle(dev); - if (ret) - DRM_ERROR("failed to idle hardware: %d\n", ret); - - i915_gem_cleanup_ringbuffer(dev); + if (dev_priv->ring.ring_obj != NULL) { + ret = i915_gem_idle(dev); + if (ret) + DRM_ERROR("failed to idle hardware: %d\n", ret); + + i915_gem_cleanup_ringbuffer(dev); + } mutex_unlock(&dev->struct_mutex); } -- cgit v1.2.3 From f4bd566e0bead0904c38bb3a526eb9b35b215ff5 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 21 Jun 2008 00:10:10 -0700 Subject: [intel-gem] Remove unused variable. --- linux-core/i915_gem.c | 1 - 1 file changed, 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 27b6ddbb..dc88df58 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -2140,7 +2140,6 @@ i915_gem_idle(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; uint32_t seqno, cur_seqno, last_seqno; - int ret; int stuck; if (dev_priv->mm.suspended) -- cgit v1.2.3 From 8be6ec491f7b9c633a426a34006ea4ff5a3f8392 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 21 Jun 2008 00:13:18 -0700 Subject: [intel-gem] Add /proc/dri/*/i915_gem_interrupt This tracks most of the interrupt-related status, including the interrupt registers in the chip and the sequence number variables. --- linux-core/i915_gem.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index dc88df58..4361b060 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -2486,15 +2486,55 @@ static int i915_gem_seqno_info(char *buf, char **start, off_t offset, } +static int i915_interrupt_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Interrupt enable: %08x\n", + I915_READ(I915REG_INT_ENABLE_R)); + DRM_PROC_PRINT("Interrupt identity: %08x\n", + I915_READ(I915REG_INT_IDENTITY_R)); + DRM_PROC_PRINT("Interrupt mask: %08x\n", + I915_READ(I915REG_INT_MASK_R)); + DRM_PROC_PRINT("Pipe A stat: %08x\n", + I915_READ(I915REG_PIPEASTAT)); + DRM_PROC_PRINT("Pipe B stat: %08x\n", + I915_READ(I915REG_PIPEBSTAT)); + DRM_PROC_PRINT("Interrupts received: %d\n", + atomic_read(&dev_priv->irq_received)); + DRM_PROC_PRINT("Current sequence: %d\n", + i915_get_gem_seqno(dev)); + DRM_PROC_PRINT("Waiter sequence: %d\n", + dev_priv->mm.waiting_gem_seqno); + DRM_PROC_PRINT("IRQ sequence: %d\n", + dev_priv->mm.irq_gem_seqno); + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + static struct drm_proc_list { const char *name; /**< file name */ int (*f) (char *, char **, off_t, int, int *, void *); /**< proc callback*/ } i915_gem_proc_list[] = { - {"gem_active", i915_gem_active_info}, - {"gem_flushing", i915_gem_flushing_info}, - {"gem_inactive", i915_gem_inactive_info}, - {"gem_request", i915_gem_request_info}, - {"gem_seqno", i915_gem_seqno_info}, + {"i915_gem_active", i915_gem_active_info}, + {"i915_gem_flushing", i915_gem_flushing_info}, + {"i915_gem_inactive", i915_gem_inactive_info}, + {"i915_gem_request", i915_gem_request_info}, + {"i915_gem_seqno", i915_gem_seqno_info}, + {"i915_gem_interrupt", i915_interrupt_info}, }; #define I915_GEM_PROC_ENTRIES ARRAY_SIZE(i915_gem_proc_list) -- cgit v1.2.3 From 71d975072cf57507385bdf8e0bf4af4c23b1fceb Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 23 Jun 2008 00:53:53 -0700 Subject: [intel-gem] pwrite through GTT Pin/copy_from_user/unpin through the GTT to eliminate clflush costs. Benchmarks say this helps quite a bit. --- linux-core/i915_gem.c | 158 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 125 insertions(+), 33 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 4361b060..4dfbd2a1 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -36,7 +36,14 @@ #define WATCH_LRU 0 #define WATCH_RELOC 0 #define WATCH_INACTIVE 0 +#define WATCH_PWRITE 0 +#if WATCH_BUF | WATCH_EXEC | WATCH_PWRITE +static void +i915_gem_dump_object(struct drm_gem_object *obj, int len, + const char *where, uint32_t mark); +#endif + static int i915_gem_object_set_domain(struct drm_gem_object *obj, uint32_t read_domains, @@ -154,6 +161,8 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, return 0; } +#include "drm_compat.h" + /** * Writes data to the object referenced by handle. * @@ -165,41 +174,121 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, { struct drm_i915_gem_pwrite *args = data; struct drm_gem_object *obj; - ssize_t written; + struct drm_i915_gem_object *obj_priv; + ssize_t remain; loff_t offset; - int ret; + char __user *user_data; + char *vaddr; + int i, o, l; + int ret = 0; + unsigned long pfn; + unsigned long unwritten; obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) return -EINVAL; + /** Bounds check destination. + * + * XXX: This could use review for overflow issues... + */ + if (args->offset > obj->size || args->size > obj->size || + args->offset + args->size > obj->size) + return -EFAULT; + + user_data = (char __user *) (uintptr_t) args->data_ptr; + remain = args->size; + if (!access_ok(VERIFY_READ, user_data, remain)) + return -EFAULT; + + mutex_lock(&dev->struct_mutex); - ret = i915_gem_set_domain(obj, file_priv, - I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU); + ret = i915_gem_object_pin(obj, 0); if (ret) { drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); return ret; } - offset = args->offset; + ret = i915_gem_set_domain(obj, file_priv, + I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT); + if (ret) + goto fail; + + obj_priv = obj->driver_private; + offset = obj_priv->gtt_offset + args->offset; + obj_priv->dirty = 1; + + while (remain > 0) { + + /** Operation in this page + * + * i = page number + * o = offset within page + * l = bytes to copy + */ + i = offset >> PAGE_SHIFT; + o = offset & (PAGE_SIZE-1); + l = remain; + if ((o + l) > PAGE_SIZE) + l = PAGE_SIZE - o; + + pfn = (dev->agp->base >> PAGE_SHIFT) + i; + +#ifdef DRM_KMAP_ATOMIC_PROT_PFN + /* kmap_atomic can't map IO pages on non-HIGHMEM kernels + */ + vaddr = kmap_atomic_prot_pfn(pfn, KM_USER0, + __pgprot(__PAGE_KERNEL)); +#if WATCH_PWRITE + DRM_INFO("pwrite i %d o %d l %d pfn %ld vaddr %p\n", + i, o, l, pfn, vaddr); +#endif + unwritten = __copy_from_user_inatomic_nocache(vaddr + o, user_data, l); + kunmap_atomic(vaddr, KM_USER0); - written = vfs_write(obj->filp, - (char __user *)(uintptr_t) args->data_ptr, - args->size, &offset); + if (unwritten) +#endif + { + vaddr = ioremap(pfn << PAGE_SHIFT, PAGE_SIZE); +#if WATCH_PWRITE + DRM_INFO("pwrite slow i %d o %d l %d pfn %ld vaddr %p\n", + i, o, l, pfn, vaddr); +#endif + if (vaddr == NULL) { + ret = -EFAULT; + goto fail; + } + unwritten = __copy_from_user(vaddr + o, user_data, l); +#if WATCH_PWRITE + DRM_INFO("unwritten %ld\n", unwritten); +#endif + iounmap(vaddr); + if (unwritten) { + ret = -EFAULT; + goto fail; + } + } - if (written != args->size) { - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - if (written < 0) - return written; - else - return -EINVAL; + remain -= l; + user_data += l; + offset += l; } +#if WATCH_PWRITE && 1 + i915_gem_clflush_object(obj); + i915_gem_dump_object(obj, args->offset + args->size, __func__, ~0); + i915_gem_clflush_object(obj); +#endif +fail: + i915_gem_object_unpin (obj); drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); - return 0; +#if WATCH_PWRITE + if (ret) + DRM_INFO("pwrite failed %d\n", ret); +#endif + return ret; } /** @@ -361,7 +450,7 @@ i915_verify_inactive(struct drm_device *dev, char *file, int line) list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { obj = obj_priv->obj; - if (obj_priv->pin_count || obj_priv->active || (obj->write_domain & ~I915_GEM_DOMAIN_CPU)) + if (obj_priv->pin_count || obj_priv->active || (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) DRM_ERROR("inactive %p (p %d a %d w %x) %s:%d\n", obj, obj_priv->pin_count, obj_priv->active, obj->write_domain, file, line); @@ -644,7 +733,7 @@ i915_gem_flush(struct drm_device *dev, if (flush_domains & I915_GEM_DOMAIN_CPU) drm_agp_chipset_flush(dev); - if ((invalidate_domains|flush_domains) & ~I915_GEM_DOMAIN_CPU) { + if ((invalidate_domains|flush_domains) & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) { /* * read/write caches: * @@ -712,7 +801,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) /* If there are writes queued to the buffer, flush and * create a new seqno to wait for. */ - if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU)) { + if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) { uint32_t write_domain = obj->write_domain; #if WATCH_BUF DRM_INFO("%s: flushing object %p from write domain %08x\n", @@ -798,11 +887,13 @@ i915_gem_object_unbind(struct drm_gem_object *obj) i915_gem_object_free_page_list(obj); - atomic_dec(&dev->gtt_count); - atomic_sub(obj->size, &dev->gtt_memory); - - drm_memrange_put_block(obj_priv->gtt_space); - obj_priv->gtt_space = NULL; + if (obj_priv->gtt_space) { + atomic_dec(&dev->gtt_count); + atomic_sub(obj->size, &dev->gtt_memory); + + drm_memrange_put_block(obj_priv->gtt_space); + obj_priv->gtt_space = NULL; + } /* Remove ourselves from the LRU list if present. */ if (!list_empty(&obj_priv->list)) @@ -811,7 +902,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) return 0; } -#if WATCH_BUF | WATCH_EXEC +#if WATCH_BUF | WATCH_EXEC | WATCH_PWRITE static void i915_gem_dump_page(struct page *page, uint32_t start, uint32_t end, uint32_t bias, uint32_t mark) @@ -1105,8 +1196,8 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) * wasn't in the GTT, there shouldn't be any way it could have been in * a GPU cache */ - BUG_ON(obj->read_domains & ~I915_GEM_DOMAIN_CPU); - BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); + BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); + BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); return 0; } @@ -1289,7 +1380,7 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, * flushed before the cpu cache is invalidated */ if ((invalidate_domains & I915_GEM_DOMAIN_CPU) && - (flush_domains & ~I915_GEM_DOMAIN_CPU)) { + (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))) { ret = i915_gem_object_wait_rendering(obj); if (ret) return ret; @@ -1911,7 +2002,7 @@ i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) if (obj_priv->pin_count == 1) { atomic_inc(&dev->pin_count); atomic_add(obj->size, &dev->pin_memory); - if (!obj_priv->active && (obj->write_domain & ~I915_GEM_DOMAIN_CPU) == 0 && + if (!obj_priv->active && (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) == 0 && !list_empty(&obj_priv->list)) list_del_init(&obj_priv->list); } @@ -1937,7 +2028,7 @@ i915_gem_object_unpin(struct drm_gem_object *obj) * the inactive list */ if (obj_priv->pin_count == 0) { - if (!obj_priv->active && (obj->write_domain & ~I915_GEM_DOMAIN_CPU) == 0) + if (!obj_priv->active && (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) == 0) list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list); atomic_dec(&dev->pin_count); @@ -2096,7 +2187,7 @@ i915_gem_set_domain(struct drm_gem_object *obj, return ret; flush_domains = i915_gem_dev_set_domain(obj->dev); - if (flush_domains & ~I915_GEM_DOMAIN_CPU) + if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) (void) i915_add_request(dev, flush_domains); return 0; @@ -2154,8 +2245,9 @@ i915_gem_idle(struct drm_device *dev) /* Flush the GPU along with all non-CPU write domains */ - i915_gem_flush(dev, ~I915_GEM_DOMAIN_CPU, ~I915_GEM_DOMAIN_CPU); - seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU); + i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT), + ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); + seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); if (seqno == 0) { mutex_unlock(&dev->struct_mutex); -- cgit v1.2.3 From ed73651d47a5f95c3436207144b70811366e4edd Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 23 Jun 2008 10:16:35 -0700 Subject: [intel-gem] Recover resources from wedged hardware. Clean up queues, free objects. On the next entervt, unmark the hardware to let the user try again (presumably after resetting the chip). Someday we'll automatically recover... --- linux-core/i915_gem.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 4dfbd2a1..16a1b07e 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -653,7 +653,7 @@ i915_gem_retire_requests(struct drm_device *dev) list); retiring_seqno = request->seqno; - if (i915_seqno_passed(seqno, retiring_seqno)) { + if (i915_seqno_passed(seqno, retiring_seqno) || dev_priv->mm.wedged) { i915_gem_retire_request(dev, request); list_del(&request->list); @@ -697,10 +697,13 @@ i915_wait_request(struct drm_device *dev, uint32_t seqno) i915_user_irq_on(dev_priv); ret = wait_event_interruptible(dev_priv->irq_queue, i915_seqno_passed(i915_get_gem_seqno(dev), - seqno)); + seqno) || dev_priv->mm.wedged); i915_user_irq_off(dev_priv); dev_priv->mm.waiting_gem_seqno = 0; } + if (dev_priv->mm.wedged) + ret = -EIO; + if (ret) DRM_ERROR("%s returns %d (awaiting %d at %d)\n", __func__, ret, seqno, i915_get_gem_seqno(dev)); @@ -1019,6 +1022,8 @@ i915_gem_evict_something(struct drm_device *dev) list); ret = i915_wait_request(dev, request->seqno); + if (ret) + break; /* if waiting caused an object to become inactive, * then loop around and wait for it. Otherwise, we @@ -1822,6 +1827,13 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); i915_verify_inactive(dev, __FILE__, __LINE__); + + if (dev_priv->mm.wedged) { + DRM_ERROR("Execbuf while wedged\n"); + mutex_unlock(&dev->struct_mutex); + return -EIO; + } + if (dev_priv->mm.suspended) { DRM_ERROR("Execbuf while VT-switched.\n"); mutex_unlock(&dev->struct_mutex); @@ -2264,6 +2276,8 @@ i915_gem_idle(struct drm_device *dev) if (last_seqno == cur_seqno) { if (stuck++ > 100) { DRM_ERROR("hardware wedged\n"); + dev_priv->mm.wedged = 1; + DRM_WAKEUP(&dev_priv->irq_queue); break; } } @@ -2378,6 +2392,11 @@ i915_gem_entervt_ioctl(struct drm_device *dev, void *data, drm_i915_private_t *dev_priv = dev->dev_private; int ret; + if (dev_priv->mm.wedged) { + DRM_ERROR("Renabling wedged hardware, good luck\n"); + dev_priv->mm.wedged = 0; + } + ret = i915_gem_init_ringbuffer(dev); if (ret != 0) return ret; -- cgit v1.2.3 From c0043155ad7199835d631e3daed5c641642c314e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 23 Jun 2008 22:03:06 -0700 Subject: drm_compat: it's CONFIG_HIGHMEM, not CONFIG_HIMEM A mis-spelled config option (was it spelled that way in the past?) eliminated kmap_atomic_prot_pfn from core DRM. --- linux-core/drm_compat.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/drm_compat.h b/linux-core/drm_compat.h index 30834f33..6aa19c55 100644 --- a/linux-core/drm_compat.h +++ b/linux-core/drm_compat.h @@ -329,7 +329,7 @@ typedef _Bool bool; #endif -#if (defined(CONFIG_X86) && defined(CONFIG_X86_32) && defined(CONFIG_HIMEM)) +#if (defined(CONFIG_X86) && defined(CONFIG_X86_32) && defined(CONFIG_HIGHMEM)) #define DRM_KMAP_ATOMIC_PROT_PFN extern void *kmap_atomic_prot_pfn(unsigned long pfn, enum km_type type, pgprot_t protection); -- cgit v1.2.3 From 2c6feb7a5a3fe60ed3961bc133ad5d6e63b8196a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 23 Jun 2008 22:03:33 -0700 Subject: [intel-gem] Include drm_compat.h to get kmap_atomic_prot_pfn --- linux-core/i915_gem.c | 1 + 1 file changed, 1 insertion(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 16a1b07e..8fd2b8a6 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -27,6 +27,7 @@ #include "drmP.h" #include "drm.h" +#include "drm_compat.h" #include "i915_drm.h" #include "i915_drv.h" -- cgit v1.2.3 From 1f9a5307acfe2ef0d104f7036d2d93504dc7673f Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 26 Jun 2008 09:35:14 -0700 Subject: [intel-gem] typo fix in DRM_ERROR --- linux-core/i915_gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 8fd2b8a6..446c9ba9 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -2394,7 +2394,7 @@ i915_gem_entervt_ioctl(struct drm_device *dev, void *data, int ret; if (dev_priv->mm.wedged) { - DRM_ERROR("Renabling wedged hardware, good luck\n"); + DRM_ERROR("Reenabling wedged hardware, good luck\n"); dev_priv->mm.wedged = 0; } -- cgit v1.2.3 From a0474be4e78d678eb615b37aad355effb955ee19 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 11 Jul 2008 14:47:33 -0700 Subject: intel-gem: Add two new ioctls for managing tiling on objects. Various chips have exciting interactions between the CPU and the GPU's different ways of accessing interleaved memory, so we need some kernel assistance in determining how it works. Only fully tested on GM965 so far. --- linux-core/Makefile.kernel | 3 +- linux-core/i915_gem.c | 15 +++ linux-core/i915_gem_tiling.c | 290 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 linux-core/i915_gem_tiling.c (limited to 'linux-core') diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index 82d200b3..abfad0a1 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -20,7 +20,8 @@ r128-objs := r128_drv.o r128_cce.o r128_state.o r128_irq.o mga-objs := mga_drv.o mga_dma.o mga_state.o mga_warp.o mga_irq.o i810-objs := i810_drv.o i810_dma.o i915-objs := i915_drv.o i915_dma.o i915_irq.o i915_mem.o i915_fence.o \ - i915_buffer.o i915_compat.o i915_execbuf.o i915_gem.o + i915_buffer.o i915_compat.o i915_execbuf.o \ + i915_gem.o i915_gem_tiling.o nouveau-objs := nouveau_drv.o nouveau_state.o nouveau_fifo.o nouveau_mem.o \ nouveau_object.o nouveau_irq.o nouveau_notifier.o nouveau_swmthd.o \ nouveau_sgdma.o nouveau_dma.o nouveau_bo.o nouveau_fence.o \ diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 446c9ba9..236203a5 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -2702,3 +2702,18 @@ i915_gem_lastclose(struct drm_device *dev) mutex_unlock(&dev->struct_mutex); } + +void i915_gem_load(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + + INIT_LIST_HEAD(&dev_priv->mm.active_list); + INIT_LIST_HEAD(&dev_priv->mm.flushing_list); + INIT_LIST_HEAD(&dev_priv->mm.inactive_list); + INIT_LIST_HEAD(&dev_priv->mm.request_list); + INIT_DELAYED_WORK(&dev_priv->mm.retire_work, + i915_gem_retire_work_handler); + dev_priv->mm.next_gem_seqno = 1; + + i915_gem_detect_bit_6_swizzle(dev); +} diff --git a/linux-core/i915_gem_tiling.c b/linux-core/i915_gem_tiling.c new file mode 100644 index 00000000..d66276d6 --- /dev/null +++ b/linux-core/i915_gem_tiling.c @@ -0,0 +1,290 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +#include "drmP.h" +#include "drm.h" +#include "i915_drm.h" +#include "i915_drv.h" + +/** @file i915_gem_tiling.c + * + * Support for managing tiling state of buffer objects. + * + * The idea behind tiling is to increase cache hit rates by rearranging + * pixel data so that a group of pixel accesses are in the same cacheline. + * Performance improvement from doing this on the back/depth buffer are on + * the order of 30%. + * + * Intel architectures make this somewhat more complicated, though, by + * adjustments made to addressing of data when the memory is in interleaved + * mode (matched pairs of DIMMS) to improve memory bandwidth. + * For interleaved memory, the CPU sends every sequential 64 bytes + * to an alternate memory channel so it can get the bandwidth from both. + * + * The GPU also rearranges its accesses for increased bandwidth to interleaved + * memory, and it matches what the CPU does for non-tiled. However, when tiled + * it does it a little differently, since one walks addresses not just in the + * X direction but also Y. So, along with alternating channels when bit + * 6 of the address flips, it also alternates when other bits flip -- Bits 9 + * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines) + * are common to both the 915 and 965-class hardware. + * + * The CPU also sometimes XORs in higher bits as well, to improve + * bandwidth doing strided access like we do so frequently in graphics. This + * is called "Channel XOR Randomization" in the MCH documentation. The result + * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address + * decode. + * + * All of this bit 6 XORing has an effect on our memory management, + * as we need to make sure that the 3d driver can correctly address object + * contents. + * + * If we don't have interleaved memory, all tiling is safe and no swizzling is + * required. + * + * When bit 17 is XORed in, we simply refuse to tile at all. Bit + * 17 is not just a page offset, so as we page an objet out and back in, + * individual pages in it will have different bit 17 addresses, resulting in + * each 64 bytes being swapped with its neighbor! + * + * Otherwise, if interleaved, we have to tell the 3d driver what the address + * swizzling it needs to do is, since it's writing with the CPU to the pages + * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the + * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling + * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order + * to match what the GPU expects. + */ + +/** + * Detects bit 6 swizzling of address lookup between IGD access and CPU + * access through main memory. + */ +void +i915_gem_detect_bit_6_swizzle(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct pci_dev *bridge; + uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN; + uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN; + int mchbar_offset; + volatile char *mchbar; + int ret; + + bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0,0)); + if (bridge == NULL) { + DRM_ERROR("Couldn't get bridge device\n"); + return; + } + + ret = pci_enable_device(bridge); + if (ret != 0) { + DRM_ERROR("pci_enable_device failed: %d\n", ret); + return; + } + + if (IS_I965G(dev)) + mchbar_offset = 0x48; + else + mchbar_offset = 0x44; + + /* Use resource 2 for our BAR that's stashed in a nonstandard location, + * since the bridge would only ever use standard BARs 0-1 (though it + * doesn't anyway) + */ + pci_read_base(bridge, mchbar_offset, &bridge->resource[2]); + + mchbar = ioremap(pci_resource_start(bridge, 2), + pci_resource_len(bridge, 2)); + if (mchbar == NULL) { + DRM_ERROR("Couldn't map MCHBAR to determine tile swizzling\n"); + return; + } + + if (IS_I965G(dev) && !IS_I965GM(dev)) { + uint32_t chdecmisc; + + /* On the 965, channel interleave appears to be determined by + * the flex bit. If flex is set, then the ranks (sides of a + * DIMM) of memory will be "stacked" (physical addresses walk + * through one rank then move on to the next, flipping channels + * or not depending on rank configuration). The GPU in this + * case does exactly the same addressing as the CPU. + * + * Unlike the 945, channel randomization based does not + * appear to be available. + * + * XXX: While the G965 doesn't appear to do any interleaving + * when the DIMMs are not exactly matched, the G4x chipsets + * might be for "L-shaped" configurations, and will need to be + * detected. + * + * L-shaped configuration: + * + * +-----+ + * | | + * |DIMM2| <-- non-interleaved + * +-----+ + * +-----+ +-----+ + * | | | | + * |DIMM0| |DIMM1| <-- interleaved area + * +-----+ +-----+ + */ + chdecmisc = readb(mchbar + CHDECMISC); + + if (chdecmisc & CHDECMISC_FLEXMEMORY) { + swizzle_x = I915_BIT_6_SWIZZLE_NONE; + swizzle_y = I915_BIT_6_SWIZZLE_NONE; + } else { + swizzle_x = I915_BIT_6_SWIZZLE_9_10; + swizzle_y = I915_BIT_6_SWIZZLE_9; + } + } else if (IS_I9XX(dev)){ + uint32_t dcc; + + /* On 915-945 and GM965, channel interleave by the CPU is + * determined by DCC. The CPU will alternate based on bit 6 + * in interleaved mode, and the GPU will then also alternate + * on bit 6, 9, and 10 for X, but the CPU may also optionally + * alternate based on bit 17 (XOR not disabled and XOR + * bit == 17). + */ + dcc = readl(mchbar + DCC); + switch (dcc & DCC_ADDRESSING_MODE_MASK) { + case DCC_ADDRESSING_MODE_SINGLE_CHANNEL: + case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC: + swizzle_x = I915_BIT_6_SWIZZLE_NONE; + swizzle_y = I915_BIT_6_SWIZZLE_NONE; + break; + case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED: + if (IS_I915G(dev) || IS_I915GM(dev) || + dcc & DCC_CHANNEL_XOR_DISABLE) { + swizzle_x = I915_BIT_6_SWIZZLE_9_10; + swizzle_y = I915_BIT_6_SWIZZLE_9; + } else if (IS_I965GM(dev)) { + /* GM965 only does bit 11-based channel + * randomization + */ + swizzle_x = I915_BIT_6_SWIZZLE_9_10_11; + swizzle_y = I915_BIT_6_SWIZZLE_9_11; + } else { + /* Bit 17 or perhaps other swizzling */ + swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN; + swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN; + } + break; + } + } + + iounmap(mchbar); + + dev_priv->mm.bit_6_swizzle_x = swizzle_x; + dev_priv->mm.bit_6_swizzle_y = swizzle_y; +} + +/** + * Sets the tiling mode of an object, returning the required swizzling of + * bit 6 of addresses in the object. + */ +int +i915_gem_set_tiling(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_set_tiling *args = data; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + obj_priv = obj->driver_private; + + mutex_lock(&dev->struct_mutex); + + if (args->tiling_mode == I915_TILING_NONE) { + obj_priv->tiling_mode = I915_TILING_NONE; + args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE; + } else { + if (args->tiling_mode == I915_TILING_X) + args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x; + else + args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y; + /* If we can't handle the swizzling, make it untiled. */ + if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) { + args->tiling_mode = I915_TILING_NONE; + args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE; + } + } + obj_priv->tiling_mode = args->tiling_mode; + + mutex_unlock(&dev->struct_mutex); + + drm_gem_object_unreference(obj); + + return 0; +} + +/** + * Returns the current tiling mode and required bit 6 swizzling for the object. + */ +int +i915_gem_get_tiling(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_get_tiling *args = data; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + obj_priv = obj->driver_private; + + mutex_lock(&dev->struct_mutex); + + args->tiling_mode = obj_priv->tiling_mode; + switch (obj_priv->tiling_mode) { + case I915_TILING_X: + args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x; + break; + case I915_TILING_Y: + args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y; + break; + case I915_TILING_NONE: + args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE; + break; + default: + DRM_ERROR("unknown tiling mode\n"); + } + + mutex_unlock(&dev->struct_mutex); + + drm_gem_object_unreference(obj); + + return 0; +} -- cgit v1.2.3 From 78f1fc9cbcb383d42d903a8b9febdcf3c438ea7c Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 14 Jul 2008 09:16:45 -0700 Subject: intel-gem: Disable tiling if we get junk from the MCHBAR read. One of our systems has been returning 0xffffffff from all MCHBAR reads, which means we'll need to figure out why, or add an alternate detection method. --- linux-core/i915_gem_tiling.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem_tiling.c b/linux-core/i915_gem_tiling.c index d66276d6..ddf83ce4 100644 --- a/linux-core/i915_gem_tiling.c +++ b/linux-core/i915_gem_tiling.c @@ -155,7 +155,10 @@ i915_gem_detect_bit_6_swizzle(struct drm_device *dev) */ chdecmisc = readb(mchbar + CHDECMISC); - if (chdecmisc & CHDECMISC_FLEXMEMORY) { + if (chdecmisc == 0xff) { + DRM_ERROR("Couldn't read from MCHBAR. " + "Disabling tiling.\n"); + } else if (chdecmisc & CHDECMISC_FLEXMEMORY) { swizzle_x = I915_BIT_6_SWIZZLE_NONE; swizzle_y = I915_BIT_6_SWIZZLE_NONE; } else { @@ -197,6 +200,12 @@ i915_gem_detect_bit_6_swizzle(struct drm_device *dev) } break; } + if (dcc == 0xffffffff) { + DRM_ERROR("Couldn't read from MCHBAR. " + "Disabling tiling.\n"); + swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN; + swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN; + } } iounmap(mchbar); -- cgit v1.2.3 From f5c2f00e2f99732311c9a35a91b6adc93047c51b Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 17 Jul 2008 16:56:42 -0700 Subject: intel-gem: Leave 8xx tiling on until we find any issues. --- linux-core/i915_gem_tiling.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem_tiling.c b/linux-core/i915_gem_tiling.c index ddf83ce4..2841ff1f 100644 --- a/linux-core/i915_gem_tiling.c +++ b/linux-core/i915_gem_tiling.c @@ -206,6 +206,12 @@ i915_gem_detect_bit_6_swizzle(struct drm_device *dev) swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN; swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN; } + } else { + /* As far as we know, the 865 doesn't have these bit 6 + * swizzling issues. + */ + swizzle_x = I915_BIT_6_SWIZZLE_NONE; + swizzle_y = I915_BIT_6_SWIZZLE_NONE; } iounmap(mchbar); -- cgit v1.2.3 From 4d83a751b421ec3f3e0c572070c3bc295b9adbcc Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Fri, 18 Jul 2008 12:42:43 -0700 Subject: drm-gem: Fix build On some distros missing prototypes cause kernel builds to fail. These are hack to make the code build. --- linux-core/i915_gem.c | 1 + linux-core/i915_gem_tiling.c | 3 +++ 2 files changed, 4 insertions(+) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 236203a5..fff2074e 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -30,6 +30,7 @@ #include "drm_compat.h" #include "i915_drm.h" #include "i915_drv.h" +#include #define WATCH_COHERENCY 0 #define WATCH_BUF 0 diff --git a/linux-core/i915_gem_tiling.c b/linux-core/i915_gem_tiling.c index 2841ff1f..c8177a7e 100644 --- a/linux-core/i915_gem_tiling.c +++ b/linux-core/i915_gem_tiling.c @@ -30,6 +30,9 @@ #include "i915_drm.h" #include "i915_drv.h" +extern int pci_read_base(struct pci_dev *dev, unsigned int reg, + struct resource *res); + /** @file i915_gem_tiling.c * * Support for managing tiling state of buffer objects. -- cgit v1.2.3 From 67d15215660407b07265c37d60ea5cac8930cef9 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 1 Jul 2008 12:31:37 -0700 Subject: intel-gem: Set up HWS when it needs a vaddr during GEM init. This requires an updated 2D driver to not try to set it up as well. --- linux-core/i915_gem.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index fff2074e..b4c478f5 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -2311,6 +2311,56 @@ i915_gem_idle(struct drm_device *dev) return 0; } +static int +i915_gem_init_hws(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + int ret; + + /* If we need a physical address for the status page, it's already + * initialized at driver load time. + */ + if (!I915_NEED_GFX_HWS(dev)) + return 0; + + obj = drm_gem_object_alloc(dev, 4096); + if (obj == NULL) { + DRM_ERROR("Failed to allocate status page\n"); + return -ENOMEM; + } + obj_priv = obj->driver_private; + + ret = i915_gem_object_pin(obj, 4096); + if (ret != 0) { + drm_gem_object_unreference(obj); + return ret; + } + + dev_priv->status_gfx_addr = obj_priv->gtt_offset; + dev_priv->hws_map.offset = dev->agp->base + obj_priv->gtt_offset; + dev_priv->hws_map.size = 4096; + dev_priv->hws_map.type = 0; + dev_priv->hws_map.flags = 0; + dev_priv->hws_map.mtrr = 0; + + drm_core_ioremap(&dev_priv->hws_map, dev); + if (dev_priv->hws_map.handle == NULL) { + DRM_ERROR("Failed to map status page.\n"); + memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map)); + drm_gem_object_unreference(obj); + return -EINVAL; + } + dev_priv->hws_obj = obj; + dev_priv->hw_status_page = dev_priv->hws_map.handle; + memset(dev_priv->hw_status_page, 0, PAGE_SIZE); + I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr); + DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr); + + return 0; +} + static int i915_gem_init_ringbuffer(struct drm_device *dev) { @@ -2319,6 +2369,10 @@ i915_gem_init_ringbuffer(struct drm_device *dev) struct drm_i915_gem_object *obj_priv; int ret; + ret = i915_gem_init_hws(dev); + if (ret != 0) + return ret; + obj = drm_gem_object_alloc(dev, 128 * 1024); if (obj == NULL) { DRM_ERROR("Failed to allocate ringbuffer\n"); @@ -2383,8 +2437,18 @@ i915_gem_cleanup_ringbuffer(struct drm_device *dev) i915_gem_object_unpin(dev_priv->ring.ring_obj); drm_gem_object_unreference(dev_priv->ring.ring_obj); - + dev_priv->ring.ring_obj = NULL; memset(&dev_priv->ring, 0, sizeof(dev_priv->ring)); + + if (dev_priv->hws_obj != NULL) { + i915_gem_object_unpin(dev_priv->hws_obj); + drm_gem_object_unreference(dev_priv->hws_obj); + dev_priv->hws_obj = NULL; + memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map)); + + /* Write high address into HWS_PGA when disabling. */ + I915_WRITE(HWS_PGA, 0x1ffff000); + } } int -- cgit v1.2.3 From e31d27857ffc1b01ca49e70b5ba6f7425414d7c8 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 21 Jul 2008 14:21:16 -0700 Subject: intel-gem: Remove recently added pci_read_base prototype. This is in pci.h in the fixed patch to the kernel. --- linux-core/i915_gem_tiling.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem_tiling.c b/linux-core/i915_gem_tiling.c index c8177a7e..2841ff1f 100644 --- a/linux-core/i915_gem_tiling.c +++ b/linux-core/i915_gem_tiling.c @@ -30,9 +30,6 @@ #include "i915_drm.h" #include "i915_drv.h" -extern int pci_read_base(struct pci_dev *dev, unsigned int reg, - struct resource *res); - /** @file i915_gem_tiling.c * * Support for managing tiling state of buffer objects. -- cgit v1.2.3 From a5d8f35f0fa651fbe8ca2897875ba188ca7dcda5 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 21 Jul 2008 14:33:55 -0700 Subject: intel-gem: Move /proc debugging to a separate file. --- linux-core/Makefile.kernel | 2 +- linux-core/i915_gem.c | 257 --------------------------------------- linux-core/i915_gem_proc.c | 290 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 291 insertions(+), 258 deletions(-) create mode 100644 linux-core/i915_gem_proc.c (limited to 'linux-core') diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index abfad0a1..dcac5f1b 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -21,7 +21,7 @@ mga-objs := mga_drv.o mga_dma.o mga_state.o mga_warp.o mga_irq.o i810-objs := i810_drv.o i810_dma.o i915-objs := i915_drv.o i915_dma.o i915_irq.o i915_mem.o i915_fence.o \ i915_buffer.o i915_compat.o i915_execbuf.o \ - i915_gem.o i915_gem_tiling.o + i915_gem.o i915_gem_proc.o i915_gem_tiling.o nouveau-objs := nouveau_drv.o nouveau_state.o nouveau_fifo.o nouveau_mem.o \ nouveau_object.o nouveau_irq.o nouveau_notifier.o nouveau_swmthd.o \ nouveau_sgdma.o nouveau_dma.o nouveau_bo.o nouveau_fence.o \ diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index b4c478f5..c5c68af9 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -2492,263 +2492,6 @@ i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, return 0; } -static int i915_gem_active_info(char *buf, char **start, off_t offset, - int request, int *eof, void *data) -{ - struct drm_minor *minor = (struct drm_minor *) data; - struct drm_device *dev = minor->dev; - drm_i915_private_t *dev_priv = dev->dev_private; - struct drm_i915_gem_object *obj_priv; - int len = 0; - - if (offset > DRM_PROC_LIMIT) { - *eof = 1; - return 0; - } - - *start = &buf[offset]; - *eof = 0; - DRM_PROC_PRINT("Active:\n"); - list_for_each_entry(obj_priv, &dev_priv->mm.active_list, - list) - { - struct drm_gem_object *obj = obj_priv->obj; - if (obj->name) { - DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", - obj, obj->name, - obj->read_domains, obj->write_domain, - obj_priv->last_rendering_seqno); - } else { - DRM_PROC_PRINT(" %p: %08x %08x %d\n", - obj, - obj->read_domains, obj->write_domain, - obj_priv->last_rendering_seqno); - } - } - if (len > request + offset) - return request; - *eof = 1; - return len - offset; -} - -static int i915_gem_flushing_info(char *buf, char **start, off_t offset, - int request, int *eof, void *data) -{ - struct drm_minor *minor = (struct drm_minor *) data; - struct drm_device *dev = minor->dev; - drm_i915_private_t *dev_priv = dev->dev_private; - struct drm_i915_gem_object *obj_priv; - int len = 0; - - if (offset > DRM_PROC_LIMIT) { - *eof = 1; - return 0; - } - - *start = &buf[offset]; - *eof = 0; - DRM_PROC_PRINT("Flushing:\n"); - list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, - list) - { - struct drm_gem_object *obj = obj_priv->obj; - if (obj->name) { - DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", - obj, obj->name, - obj->read_domains, obj->write_domain, - obj_priv->last_rendering_seqno); - } else { - DRM_PROC_PRINT(" %p: %08x %08x %d\n", obj, - obj->read_domains, obj->write_domain, - obj_priv->last_rendering_seqno); - } - } - if (len > request + offset) - return request; - *eof = 1; - return len - offset; -} - -static int i915_gem_inactive_info(char *buf, char **start, off_t offset, - int request, int *eof, void *data) -{ - struct drm_minor *minor = (struct drm_minor *) data; - struct drm_device *dev = minor->dev; - drm_i915_private_t *dev_priv = dev->dev_private; - struct drm_i915_gem_object *obj_priv; - int len = 0; - - if (offset > DRM_PROC_LIMIT) { - *eof = 1; - return 0; - } - - *start = &buf[offset]; - *eof = 0; - DRM_PROC_PRINT("Inactive:\n"); - list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, - list) - { - struct drm_gem_object *obj = obj_priv->obj; - if (obj->name) { - DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", - obj, obj->name, - obj->read_domains, obj->write_domain, - obj_priv->last_rendering_seqno); - } else { - DRM_PROC_PRINT(" %p: %08x %08x %d\n", obj, - obj->read_domains, obj->write_domain, - obj_priv->last_rendering_seqno); - } - } - if (len > request + offset) - return request; - *eof = 1; - return len - offset; -} - -static int i915_gem_request_info(char *buf, char **start, off_t offset, - int request, int *eof, void *data) -{ - struct drm_minor *minor = (struct drm_minor *) data; - struct drm_device *dev = minor->dev; - drm_i915_private_t *dev_priv = dev->dev_private; - struct drm_i915_gem_request *gem_request; - int len = 0; - - if (offset > DRM_PROC_LIMIT) { - *eof = 1; - return 0; - } - - *start = &buf[offset]; - *eof = 0; - DRM_PROC_PRINT("Request:\n"); - list_for_each_entry(gem_request, &dev_priv->mm.request_list, - list) - { - DRM_PROC_PRINT (" %d @ %d %08x\n", - gem_request->seqno, - (int) (jiffies - gem_request->emitted_jiffies), - gem_request->flush_domains); - } - if (len > request + offset) - return request; - *eof = 1; - return len - offset; -} - -static int i915_gem_seqno_info(char *buf, char **start, off_t offset, - int request, int *eof, void *data) -{ - struct drm_minor *minor = (struct drm_minor *) data; - struct drm_device *dev = minor->dev; - drm_i915_private_t *dev_priv = dev->dev_private; - int len = 0; - - if (offset > DRM_PROC_LIMIT) { - *eof = 1; - return 0; - } - - *start = &buf[offset]; - *eof = 0; - DRM_PROC_PRINT("Current sequence: %d\n", i915_get_gem_seqno(dev)); - DRM_PROC_PRINT("Waiter sequence: %d\n", dev_priv->mm.waiting_gem_seqno); - DRM_PROC_PRINT("IRQ sequence: %d\n", dev_priv->mm.irq_gem_seqno); - if (len > request + offset) - return request; - *eof = 1; - return len - offset; -} - - -static int i915_interrupt_info(char *buf, char **start, off_t offset, - int request, int *eof, void *data) -{ - struct drm_minor *minor = (struct drm_minor *) data; - struct drm_device *dev = minor->dev; - drm_i915_private_t *dev_priv = dev->dev_private; - int len = 0; - - if (offset > DRM_PROC_LIMIT) { - *eof = 1; - return 0; - } - - *start = &buf[offset]; - *eof = 0; - DRM_PROC_PRINT("Interrupt enable: %08x\n", - I915_READ(I915REG_INT_ENABLE_R)); - DRM_PROC_PRINT("Interrupt identity: %08x\n", - I915_READ(I915REG_INT_IDENTITY_R)); - DRM_PROC_PRINT("Interrupt mask: %08x\n", - I915_READ(I915REG_INT_MASK_R)); - DRM_PROC_PRINT("Pipe A stat: %08x\n", - I915_READ(I915REG_PIPEASTAT)); - DRM_PROC_PRINT("Pipe B stat: %08x\n", - I915_READ(I915REG_PIPEBSTAT)); - DRM_PROC_PRINT("Interrupts received: %d\n", - atomic_read(&dev_priv->irq_received)); - DRM_PROC_PRINT("Current sequence: %d\n", - i915_get_gem_seqno(dev)); - DRM_PROC_PRINT("Waiter sequence: %d\n", - dev_priv->mm.waiting_gem_seqno); - DRM_PROC_PRINT("IRQ sequence: %d\n", - dev_priv->mm.irq_gem_seqno); - if (len > request + offset) - return request; - *eof = 1; - return len - offset; -} - -static struct drm_proc_list { - const char *name; /**< file name */ - int (*f) (char *, char **, off_t, int, int *, void *); /**< proc callback*/ -} i915_gem_proc_list[] = { - {"i915_gem_active", i915_gem_active_info}, - {"i915_gem_flushing", i915_gem_flushing_info}, - {"i915_gem_inactive", i915_gem_inactive_info}, - {"i915_gem_request", i915_gem_request_info}, - {"i915_gem_seqno", i915_gem_seqno_info}, - {"i915_gem_interrupt", i915_interrupt_info}, -}; - -#define I915_GEM_PROC_ENTRIES ARRAY_SIZE(i915_gem_proc_list) - -int i915_gem_proc_init(struct drm_minor *minor) -{ - struct proc_dir_entry *ent; - int i, j; - - for (i = 0; i < I915_GEM_PROC_ENTRIES; i++) { - ent = create_proc_entry(i915_gem_proc_list[i].name, - S_IFREG | S_IRUGO, minor->dev_root); - if (!ent) { - DRM_ERROR("Cannot create /proc/dri/.../%s\n", - i915_gem_proc_list[i].name); - for (j = 0; j < i; j++) - remove_proc_entry(i915_gem_proc_list[i].name, - minor->dev_root); - return -1; - } - ent->read_proc = i915_gem_proc_list[i].f; - ent->data = minor; - } - return 0; -} - -void i915_gem_proc_cleanup(struct drm_minor *minor) -{ - int i; - - if (!minor->dev_root) - return; - - for (i = 0; i < I915_GEM_PROC_ENTRIES; i++) - remove_proc_entry(i915_gem_proc_list[i].name, minor->dev_root); -} - void i915_gem_lastclose(struct drm_device *dev) { diff --git a/linux-core/i915_gem_proc.c b/linux-core/i915_gem_proc.c new file mode 100644 index 00000000..41060a4b --- /dev/null +++ b/linux-core/i915_gem_proc.c @@ -0,0 +1,290 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * Keith Packard + * + */ + +#include "drmP.h" +#include "drm.h" +#include "drm_compat.h" +#include "i915_drm.h" +#include "i915_drv.h" + +static int i915_gem_active_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Active:\n"); + list_for_each_entry(obj_priv, &dev_priv->mm.active_list, + list) + { + struct drm_gem_object *obj = obj_priv->obj; + if (obj->name) { + DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", + obj, obj->name, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } else { + DRM_PROC_PRINT(" %p: %08x %08x %d\n", + obj, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } + } + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static int i915_gem_flushing_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Flushing:\n"); + list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, + list) + { + struct drm_gem_object *obj = obj_priv->obj; + if (obj->name) { + DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", + obj, obj->name, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } else { + DRM_PROC_PRINT(" %p: %08x %08x %d\n", obj, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } + } + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static int i915_gem_inactive_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Inactive:\n"); + list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, + list) + { + struct drm_gem_object *obj = obj_priv->obj; + if (obj->name) { + DRM_PROC_PRINT(" %p(%d): %08x %08x %d\n", + obj, obj->name, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } else { + DRM_PROC_PRINT(" %p: %08x %08x %d\n", obj, + obj->read_domains, obj->write_domain, + obj_priv->last_rendering_seqno); + } + } + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static int i915_gem_request_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_request *gem_request; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Request:\n"); + list_for_each_entry(gem_request, &dev_priv->mm.request_list, + list) + { + DRM_PROC_PRINT (" %d @ %d %08x\n", + gem_request->seqno, + (int) (jiffies - gem_request->emitted_jiffies), + gem_request->flush_domains); + } + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static int i915_gem_seqno_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Current sequence: %d\n", i915_get_gem_seqno(dev)); + DRM_PROC_PRINT("Waiter sequence: %d\n", dev_priv->mm.waiting_gem_seqno); + DRM_PROC_PRINT("IRQ sequence: %d\n", dev_priv->mm.irq_gem_seqno); + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + + +static int i915_interrupt_info(char *buf, char **start, off_t offset, + int request, int *eof, void *data) +{ + struct drm_minor *minor = (struct drm_minor *) data; + struct drm_device *dev = minor->dev; + drm_i915_private_t *dev_priv = dev->dev_private; + int len = 0; + + if (offset > DRM_PROC_LIMIT) { + *eof = 1; + return 0; + } + + *start = &buf[offset]; + *eof = 0; + DRM_PROC_PRINT("Interrupt enable: %08x\n", + I915_READ(I915REG_INT_ENABLE_R)); + DRM_PROC_PRINT("Interrupt identity: %08x\n", + I915_READ(I915REG_INT_IDENTITY_R)); + DRM_PROC_PRINT("Interrupt mask: %08x\n", + I915_READ(I915REG_INT_MASK_R)); + DRM_PROC_PRINT("Pipe A stat: %08x\n", + I915_READ(I915REG_PIPEASTAT)); + DRM_PROC_PRINT("Pipe B stat: %08x\n", + I915_READ(I915REG_PIPEBSTAT)); + DRM_PROC_PRINT("Interrupts received: %d\n", + atomic_read(&dev_priv->irq_received)); + DRM_PROC_PRINT("Current sequence: %d\n", + i915_get_gem_seqno(dev)); + DRM_PROC_PRINT("Waiter sequence: %d\n", + dev_priv->mm.waiting_gem_seqno); + DRM_PROC_PRINT("IRQ sequence: %d\n", + dev_priv->mm.irq_gem_seqno); + if (len > request + offset) + return request; + *eof = 1; + return len - offset; +} + +static struct drm_proc_list { + const char *name; /**< file name */ + int (*f) (char *, char **, off_t, int, int *, void *); /**< proc callback*/ +} i915_gem_proc_list[] = { + {"i915_gem_active", i915_gem_active_info}, + {"i915_gem_flushing", i915_gem_flushing_info}, + {"i915_gem_inactive", i915_gem_inactive_info}, + {"i915_gem_request", i915_gem_request_info}, + {"i915_gem_seqno", i915_gem_seqno_info}, + {"i915_gem_interrupt", i915_interrupt_info}, +}; + +#define I915_GEM_PROC_ENTRIES ARRAY_SIZE(i915_gem_proc_list) + +int i915_gem_proc_init(struct drm_minor *minor) +{ + struct proc_dir_entry *ent; + int i, j; + + for (i = 0; i < I915_GEM_PROC_ENTRIES; i++) { + ent = create_proc_entry(i915_gem_proc_list[i].name, + S_IFREG | S_IRUGO, minor->dev_root); + if (!ent) { + DRM_ERROR("Cannot create /proc/dri/.../%s\n", + i915_gem_proc_list[i].name); + for (j = 0; j < i; j++) + remove_proc_entry(i915_gem_proc_list[i].name, + minor->dev_root); + return -1; + } + ent->read_proc = i915_gem_proc_list[i].f; + ent->data = minor; + } + return 0; +} + +void i915_gem_proc_cleanup(struct drm_minor *minor) +{ + int i; + + if (!minor->dev_root) + return; + + for (i = 0; i < I915_GEM_PROC_ENTRIES; i++) + remove_proc_entry(i915_gem_proc_list[i].name, minor->dev_root); +} -- cgit v1.2.3 From bddb952578d58c4dcfafe969c045a39d27666b56 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 23 Jul 2008 08:36:54 -0700 Subject: intel-gem: Don't do the GTT-pwrite shortcut on tiled buffers. These will be covered by the fence, while pread/pwrite are supposed to be CPU-perspective writes, with manual detiling done by the client. --- linux-core/i915_gem.c | 110 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 85 insertions(+), 25 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index c5c68af9..ca2dd19c 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -165,18 +165,12 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, #include "drm_compat.h" -/** - * Writes data to the object referenced by handle. - * - * On error, the contents of the buffer that were to be modified are undefined. - */ -int -i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, - struct drm_file *file_priv) +static int +i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj, + struct drm_i915_gem_pwrite *args, + struct drm_file *file_priv) { - struct drm_i915_gem_pwrite *args = data; - struct drm_gem_object *obj; - struct drm_i915_gem_object *obj_priv; + struct drm_i915_gem_object *obj_priv = obj->driver_private; ssize_t remain; loff_t offset; char __user *user_data; @@ -186,18 +180,6 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, unsigned long pfn; unsigned long unwritten; - obj = drm_gem_object_lookup(dev, file_priv, args->handle); - if (obj == NULL) - return -EINVAL; - - /** Bounds check destination. - * - * XXX: This could use review for overflow issues... - */ - if (args->offset > obj->size || args->size > obj->size || - args->offset + args->size > obj->size) - return -EFAULT; - user_data = (char __user *) (uintptr_t) args->data_ptr; remain = args->size; if (!access_ok(VERIFY_READ, user_data, remain)) @@ -207,7 +189,6 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); ret = i915_gem_object_pin(obj, 0); if (ret) { - drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); return ret; } @@ -283,13 +264,92 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, fail: i915_gem_object_unpin (obj); - drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); + return ret; +} + +int +i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj, + struct drm_i915_gem_pwrite *args, + struct drm_file *file_priv) +{ + int ret; + loff_t offset; + ssize_t written; + + mutex_lock(&dev->struct_mutex); + + ret = i915_gem_set_domain(obj, file_priv, + I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU); + if (ret) { + mutex_unlock(&dev->struct_mutex); + return ret; + } + + offset = args->offset; + + written = vfs_write(obj->filp, + (char __user *)(uintptr_t) args->data_ptr, + args->size, &offset); + if (written != args->size) { + mutex_unlock(&dev->struct_mutex); + if (written < 0) + return written; + else + return -EINVAL; + } + + mutex_unlock(&dev->struct_mutex); + + return 0; +} + +/** + * Writes data to the object referenced by handle. + * + * On error, the contents of the buffer that were to be modified are undefined. + */ +int +i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv) +{ + struct drm_i915_gem_pwrite *args = data; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + int ret = 0; + + obj = drm_gem_object_lookup(dev, file_priv, args->handle); + if (obj == NULL) + return -EINVAL; + obj_priv = obj->driver_private; + + /** Bounds check destination. + * + * XXX: This could use review for overflow issues... + */ + if (args->offset > obj->size || args->size > obj->size || + args->offset + args->size > obj->size) + return -EFAULT; + + /* We can only do the GTT pwrite on untiled buffers, as otherwise + * it would end up going through the fenced access, and we'll get + * different detiling behavior between reading and writing. + * pread/pwrite currently are reading and writing from the CPU + * perspective, requiring manual detiling by the client. + */ + if (obj_priv->tiling_mode == I915_TILING_NONE) + ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv); + else + ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv); + #if WATCH_PWRITE if (ret) DRM_INFO("pwrite failed %d\n", ret); #endif + + drm_gem_object_unreference(obj); + return ret; } -- cgit v1.2.3 From 439d7106832f2e9742deb900d96f1d3bc07162b1 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 23 Jul 2008 10:07:16 -0700 Subject: intel-gem: Add a quick hack to reduce clflushing on pread. This increases overhead for the large-readpixels case due to the repeated page cache accessing, but greatly reduces overhead for the small-readpixels case. --- linux-core/i915_gem.c | 57 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index ca2dd19c..db068ce3 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -55,6 +55,9 @@ i915_gem_set_domain(struct drm_gem_object *obj, struct drm_file *file_priv, uint32_t read_domains, uint32_t write_domain); +static int i915_gem_object_get_page_list(struct drm_gem_object *obj); +static void i915_gem_object_free_page_list(struct drm_gem_object *obj); +static int i915_gem_object_wait_rendering(struct drm_gem_object *obj); static void i915_gem_clflush_object(struct drm_gem_object *obj); @@ -128,6 +131,7 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, { struct drm_i915_gem_pread *args = data; struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; ssize_t read; loff_t offset; int ret; @@ -135,15 +139,52 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) return -EINVAL; + obj_priv = obj->driver_private; - mutex_lock(&dev->struct_mutex); - ret = i915_gem_set_domain(obj, file_priv, - I915_GEM_DOMAIN_CPU, 0); - if (ret) { + /* Bounds check source. + * + * XXX: This could use review for overflow issues... + */ + if (args->offset > obj->size || args->size > obj->size || + args->offset + args->size > obj->size) { drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - return ret; + return -EFAULT; + } + + mutex_lock(&dev->struct_mutex); + + /* Do a partial equivalent of i915_gem_set_domain(CPU, 0), as + * we don't want to clflush whole objects to read a portion of them. + * + * The side effect of doing this is that repeated preads of the same + * contents would take extra clflush overhead, since we don't track + * flushedness on a page basis. + */ + if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) { + ret = i915_gem_object_wait_rendering(obj); + if (ret) { + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); + return ret; + } } + if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { + int got_page_list = 0; + int first_page = args->offset / PAGE_SIZE; + int last_page = (args->offset + args->size) / PAGE_SIZE; + + if (obj_priv->page_list == NULL) { + i915_gem_object_get_page_list(obj); + got_page_list = 1; + } + + drm_ttm_cache_flush(&obj_priv->page_list[first_page], + last_page - first_page + 1); + + if (got_page_list) + i915_gem_object_free_page_list(obj); + } + offset = args->offset; read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr, @@ -329,8 +370,10 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, * XXX: This could use review for overflow issues... */ if (args->offset > obj->size || args->size > obj->size || - args->offset + args->size > obj->size) + args->offset + args->size > obj->size) { + drm_gem_object_unreference(obj); return -EFAULT; + } /* We can only do the GTT pwrite on untiled buffers, as otherwise * it would end up going through the fenced access, and we'll get -- cgit v1.2.3 From 6d258ddf7715412e2fb6fae35ea28d49c57ee130 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 23 Jul 2008 11:49:25 -0700 Subject: intel-gem: Fix pread math and logic errors. Fixes an oops in fbotexture from walking off the end of the page list. --- linux-core/i915_gem.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index db068ce3..e4697427 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -169,20 +169,16 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, } } if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { - int got_page_list = 0; int first_page = args->offset / PAGE_SIZE; - int last_page = (args->offset + args->size) / PAGE_SIZE; + int last_page = (args->offset + args->size - 1) / PAGE_SIZE; - if (obj_priv->page_list == NULL) { - i915_gem_object_get_page_list(obj); - got_page_list = 1; - } + /* If we don't have the page list, the pages are unpinned + * and swappable, and thus should already be in the CPU domain. + */ + BUG_ON(obj_priv->page_list == NULL); drm_ttm_cache_flush(&obj_priv->page_list[first_page], last_page - first_page + 1); - - if (got_page_list) - i915_gem_object_free_page_list(obj); } offset = args->offset; -- cgit v1.2.3 From 04ae66db1c517264cddc786be962fdd393c9c8ac Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Sat, 26 Jul 2008 19:51:58 -0700 Subject: intel-gem: Move debug-only functions to a separate file. --- linux-core/Makefile.kernel | 2 +- linux-core/i915_gem.c | 185 +-------------------------------------------- 2 files changed, 2 insertions(+), 185 deletions(-) (limited to 'linux-core') diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index dcac5f1b..b114dee5 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -21,7 +21,7 @@ mga-objs := mga_drv.o mga_dma.o mga_state.o mga_warp.o mga_irq.o i810-objs := i810_drv.o i810_dma.o i915-objs := i915_drv.o i915_dma.o i915_irq.o i915_mem.o i915_fence.o \ i915_buffer.o i915_compat.o i915_execbuf.o \ - i915_gem.o i915_gem_proc.o i915_gem_tiling.o + i915_gem.o i915_gem_debug.o i915_gem_proc.o i915_gem_tiling.o nouveau-objs := nouveau_drv.o nouveau_state.o nouveau_fifo.o nouveau_mem.o \ nouveau_object.o nouveau_irq.o nouveau_notifier.o nouveau_swmthd.o \ nouveau_sgdma.o nouveau_dma.o nouveau_bo.o nouveau_fence.o \ diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index e4697427..4087854c 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -32,20 +32,6 @@ #include "i915_drv.h" #include -#define WATCH_COHERENCY 0 -#define WATCH_BUF 0 -#define WATCH_EXEC 0 -#define WATCH_LRU 0 -#define WATCH_RELOC 0 -#define WATCH_INACTIVE 0 -#define WATCH_PWRITE 0 - -#if WATCH_BUF | WATCH_EXEC | WATCH_PWRITE -static void -i915_gem_dump_object(struct drm_gem_object *obj, int len, - const char *where, uint32_t mark); -#endif - static int i915_gem_object_set_domain(struct drm_gem_object *obj, uint32_t read_domains, @@ -59,9 +45,6 @@ static int i915_gem_object_get_page_list(struct drm_gem_object *obj); static void i915_gem_object_free_page_list(struct drm_gem_object *obj); static int i915_gem_object_wait_rendering(struct drm_gem_object *obj); -static void -i915_gem_clflush_object(struct drm_gem_object *obj); - int i915_gem_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -541,25 +524,6 @@ i915_gem_object_move_to_active(struct drm_gem_object *obj) &dev_priv->mm.active_list); } -#if WATCH_INACTIVE -static void -i915_verify_inactive(struct drm_device *dev, char *file, int line) -{ - drm_i915_private_t *dev_priv = dev->dev_private; - struct drm_gem_object *obj; - struct drm_i915_gem_object *obj_priv; - - list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { - obj = obj_priv->obj; - if (obj_priv->pin_count || obj_priv->active || (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) - DRM_ERROR("inactive %p (p %d a %d w %x) %s:%d\n", - obj, - obj_priv->pin_count, obj_priv->active, obj->write_domain, file, line); - } -} -#else -#define i915_verify_inactive(dev,file,line) -#endif static void i915_gem_object_move_to_inactive(struct drm_gem_object *obj) @@ -1006,83 +970,6 @@ i915_gem_object_unbind(struct drm_gem_object *obj) return 0; } -#if WATCH_BUF | WATCH_EXEC | WATCH_PWRITE -static void -i915_gem_dump_page(struct page *page, uint32_t start, uint32_t end, - uint32_t bias, uint32_t mark) -{ - uint32_t *mem = kmap_atomic(page, KM_USER0); - int i; - for (i = start; i < end; i += 4) - DRM_INFO("%08x: %08x%s\n", - (int) (bias + i), mem[i / 4], - (bias + i == mark) ? " ********" : ""); - kunmap_atomic(mem, KM_USER0); - /* give syslog time to catch up */ - msleep(1); -} - -static void -i915_gem_dump_object(struct drm_gem_object *obj, int len, - const char *where, uint32_t mark) -{ - struct drm_i915_gem_object *obj_priv = obj->driver_private; - int page; - - DRM_INFO("%s: object at offset %08x\n", where, obj_priv->gtt_offset); - for (page = 0; page < (len + PAGE_SIZE-1) / PAGE_SIZE; page++) { - int page_len, chunk, chunk_len; - - page_len = len - page * PAGE_SIZE; - if (page_len > PAGE_SIZE) - page_len = PAGE_SIZE; - - for (chunk = 0; chunk < page_len; chunk += 128) { - chunk_len = page_len - chunk; - if (chunk_len > 128) - chunk_len = 128; - i915_gem_dump_page(obj_priv->page_list[page], - chunk, chunk + chunk_len, - obj_priv->gtt_offset + - page * PAGE_SIZE, - mark); - } - } -} -#endif - -#if WATCH_LRU -static void -i915_dump_lru(struct drm_device *dev, const char *where) -{ - drm_i915_private_t *dev_priv = dev->dev_private; - struct drm_i915_gem_object *obj_priv; - - DRM_INFO("active list %s {\n", where); - list_for_each_entry(obj_priv, &dev_priv->mm.active_list, - list) - { - DRM_INFO(" %p: %08x\n", obj_priv, - obj_priv->last_rendering_seqno); - } - DRM_INFO("}\n"); - DRM_INFO("flushing list %s {\n", where); - list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, - list) - { - DRM_INFO(" %p: %08x\n", obj_priv, - obj_priv->last_rendering_seqno); - } - DRM_INFO("}\n"); - DRM_INFO("inactive %s {\n", where); - list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { - DRM_INFO(" %p: %08x\n", obj_priv, - obj_priv->last_rendering_seqno); - } - DRM_INFO("}\n"); -} -#endif - static int i915_gem_evict_something(struct drm_device *dev) { @@ -1308,7 +1195,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) return 0; } -static void +void i915_gem_clflush_object(struct drm_gem_object *obj) { struct drm_i915_gem_object *obj_priv = obj->driver_private; @@ -1540,76 +1427,6 @@ i915_gem_dev_set_domain(struct drm_device *dev) return flush_domains; } -#if WATCH_COHERENCY -static void -i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle) -{ - struct drm_device *dev = obj->dev; - struct drm_i915_gem_object *obj_priv = obj->driver_private; - int page; - uint32_t *gtt_mapping; - uint32_t *backing_map = NULL; - int bad_count = 0; - - DRM_INFO("%s: checking coherency of object %p@0x%08x (%d, %dkb):\n", - __func__, obj, obj_priv->gtt_offset, handle, - obj->size / 1024); - - gtt_mapping = ioremap(dev->agp->base + obj_priv->gtt_offset, - obj->size); - if (gtt_mapping == NULL) { - DRM_ERROR("failed to map GTT space\n"); - return; - } - - for (page = 0; page < obj->size / PAGE_SIZE; page++) { - int i; - - backing_map = kmap_atomic(obj_priv->page_list[page], KM_USER0); - - if (backing_map == NULL) { - DRM_ERROR("failed to map backing page\n"); - goto out; - } - - for (i = 0; i < PAGE_SIZE / 4; i++) { - uint32_t cpuval = backing_map[i]; - uint32_t gttval = readl(gtt_mapping + - page * 1024 + i); - - if (cpuval != gttval) { - DRM_INFO("incoherent CPU vs GPU at 0x%08x: " - "0x%08x vs 0x%08x\n", - (int)(obj_priv->gtt_offset + - page * PAGE_SIZE + i * 4), - cpuval, gttval); - if (bad_count++ >= 8) { - DRM_INFO("...\n"); - goto out; - } - } - } - kunmap_atomic(backing_map, KM_USER0); - backing_map = NULL; - } - - out: - if (backing_map != NULL) - kunmap_atomic(backing_map, KM_USER0); - iounmap(gtt_mapping); - - /* give syslog time to catch up */ - msleep(1); - - /* Directly flush the object, since we just loaded values with the CPU - * from thebacking pages and we don't want to disturb the cache - * management that we're trying to observe. - */ - - i915_gem_clflush_object(obj); -} -#endif - /** * Pin an object to the GTT and evaluate the relocations landing in it. */ -- cgit v1.2.3 From f85fd1b42dc2d77266007c02144d4f4f524e4157 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Sat, 26 Jul 2008 19:28:14 -0700 Subject: intel-gem: Speed up tiled readpixels by tracking which pages have been flushed. This is around 3x or so speedup, since we would read wide rows at a time, and clflush each tile 8 times as a result. We'll want code related to this anyway when we do fault-based per-page clflushing for sw fallbacks. --- linux-core/i915_gem.c | 99 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 26 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 4087854c..eea2d488 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -36,6 +36,12 @@ static int i915_gem_object_set_domain(struct drm_gem_object *obj, uint32_t read_domains, uint32_t write_domain); +static int +i915_gem_object_set_domain_range(struct drm_gem_object *obj, + uint64_t offset, + uint64_t size, + uint32_t read_domains, + uint32_t write_domain); int i915_gem_set_domain(struct drm_gem_object *obj, struct drm_file *file_priv, @@ -136,32 +142,11 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, mutex_lock(&dev->struct_mutex); - /* Do a partial equivalent of i915_gem_set_domain(CPU, 0), as - * we don't want to clflush whole objects to read a portion of them. - * - * The side effect of doing this is that repeated preads of the same - * contents would take extra clflush overhead, since we don't track - * flushedness on a page basis. - */ - if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) { - ret = i915_gem_object_wait_rendering(obj); - if (ret) { - drm_gem_object_unreference(obj); - mutex_unlock(&dev->struct_mutex); - return ret; - } - } - if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { - int first_page = args->offset / PAGE_SIZE; - int last_page = (args->offset + args->size - 1) / PAGE_SIZE; - - /* If we don't have the page list, the pages are unpinned - * and swappable, and thus should already be in the CPU domain. - */ - BUG_ON(obj_priv->page_list == NULL); - - drm_ttm_cache_flush(&obj_priv->page_list[first_page], - last_page - first_page + 1); + ret = i915_gem_object_set_domain_range(obj, args->offset, args->size, + I915_GEM_DOMAIN_CPU, 0); + if (ret != 0) { + drm_gem_object_unreference(obj); + mutex_unlock(&dev->struct_mutex); } offset = args->offset; @@ -1383,7 +1368,17 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, if ((write_domain | flush_domains) != 0) obj->write_domain = write_domain; + + /* If we're invalidating the CPU domain, clear the per-page CPU + * domain list as well. + */ + if (obj_priv->page_cpu_valid != NULL && + (obj->read_domains & I915_GEM_DOMAIN_CPU) && + ((read_domains & I915_GEM_DOMAIN_CPU) == 0)) { + memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE); + } obj->read_domains = read_domains; + dev->invalidate_domains |= invalidate_domains; dev->flush_domains |= flush_domains; #if WATCH_BUF @@ -1395,6 +1390,57 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, return 0; } +/** + * Set the read/write domain on a range of the object. + * + * Currently only implemented for CPU reads, otherwise drops to normal + * i915_gem_object_set_domain(). + */ +static int +i915_gem_object_set_domain_range(struct drm_gem_object *obj, + uint64_t offset, + uint64_t size, + uint32_t read_domains, + uint32_t write_domain) +{ + struct drm_i915_gem_object *obj_priv = obj->driver_private; + int ret, i; + + if (obj->read_domains & I915_GEM_DOMAIN_CPU) + return 0; + + if (read_domains != I915_GEM_DOMAIN_CPU || + write_domain != 0) + return i915_gem_object_set_domain(obj, + read_domains, write_domain); + + /* Wait on any GPU rendering to the object to be flushed. */ + if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) { + ret = i915_gem_object_wait_rendering(obj); + if (ret) + return ret; + } + + if (obj_priv->page_cpu_valid == NULL) { + obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE, + DRM_MEM_DRIVER); + } + + /* Flush the cache on any pages that are still invalid from the CPU's + * perspective. + */ + for (i = offset / PAGE_SIZE; i < (offset + size - 1) / PAGE_SIZE; i++) { + if (obj_priv->page_cpu_valid[i]) + continue; + + drm_ttm_cache_flush(obj_priv->page_list + i, 1); + + obj_priv->page_cpu_valid[i] = 1; + } + + return 0; +} + /** * Once all of the objects have been set in the proper domain, * perform the necessary flush and invalidate operations. @@ -2097,6 +2143,7 @@ void i915_gem_free_object(struct drm_gem_object *obj) i915_gem_object_unbind(obj); + drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER); drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); } -- cgit v1.2.3 From 1bdf35fe19c1aa02b301375b3cae7ad29adacef8 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 28 Jul 2008 11:24:00 -0700 Subject: intel-gem: Fix regression tests. Main fix is an oops that was triggered by the gtt pwrite path when we don't have the gtt initialized. Also, settle on -EBADF for "bad object handle", and -EINVAL for "reading/writing beyond object boundary". --- linux-core/i915_gem.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index eea2d488..4f207d6d 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -127,7 +127,7 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) - return -EINVAL; + return -EBADF; obj_priv = obj->driver_private; /* Bounds check source. @@ -137,7 +137,7 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, if (args->offset > obj->size || args->size > obj->size || args->offset + args->size > obj->size) { drm_gem_object_unreference(obj); - return -EFAULT; + return -EINVAL; } mutex_lock(&dev->struct_mutex); @@ -326,7 +326,7 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) - return -EINVAL; + return -EBADF; obj_priv = obj->driver_private; /** Bounds check destination. @@ -336,7 +336,7 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, if (args->offset > obj->size || args->size > obj->size || args->offset + args->size > obj->size) { drm_gem_object_unreference(obj); - return -EFAULT; + return -EINVAL; } /* We can only do the GTT pwrite on untiled buffers, as otherwise @@ -345,7 +345,8 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, * pread/pwrite currently are reading and writing from the CPU * perspective, requiring manual detiling by the client. */ - if (obj_priv->tiling_mode == I915_TILING_NONE) + if (obj_priv->tiling_mode == I915_TILING_NONE && + dev->gtt_total != 0) ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv); else ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv); @@ -376,7 +377,7 @@ i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) - return -EINVAL; + return -EBADF; mutex_lock(&dev->struct_mutex); ret = i915_gem_set_domain(obj, file_priv, @@ -405,7 +406,7 @@ i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) { mutex_unlock(&dev->struct_mutex); - return -EINVAL; + return -EBADF; } #if WATCH_BUF @@ -446,7 +447,7 @@ i915_gem_mmap_ioctl(struct drm_device *dev, void *data, obj = drm_gem_object_lookup(dev, file_priv, args->handle); if (obj == NULL) - return -EINVAL; + return -EBADF; offset = args->offset; @@ -1517,7 +1518,7 @@ i915_gem_object_pin_and_relocate(struct drm_gem_object *obj, reloc.target_handle); if (target_obj == NULL) { i915_gem_object_unpin(obj); - return -EINVAL; + return -EBADF; } target_obj_priv = target_obj->driver_private; @@ -1818,7 +1819,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, if (object_list[i] == NULL) { DRM_ERROR("Invalid object handle %d at index %d\n", exec_list[i].handle, i); - ret = -EINVAL; + ret = -EBADF; goto err; } @@ -2029,7 +2030,7 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n", args->handle); mutex_unlock(&dev->struct_mutex); - return -EINVAL; + return -EBADF; } obj_priv = obj->driver_private; @@ -2069,7 +2070,7 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data, DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n", args->handle); mutex_unlock(&dev->struct_mutex); - return -EINVAL; + return -EBADF; } i915_gem_object_unpin(obj); @@ -2093,7 +2094,7 @@ i915_gem_busy_ioctl(struct drm_device *dev, void *data, DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n", args->handle); mutex_unlock(&dev->struct_mutex); - return -EINVAL; + return -EBADF; } obj_priv = obj->driver_private; -- cgit v1.2.3 From 487c42bd42d93304278abce03b36c935bdc83284 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 28 Jul 2008 11:45:22 -0700 Subject: intel-gem: Another checkpatch.pl pass. --- linux-core/i915_gem.c | 92 ++++++++++++++++++++++++-------------------- linux-core/i915_gem_proc.c | 29 +++++++------- linux-core/i915_gem_tiling.c | 6 +-- 3 files changed, 70 insertions(+), 57 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 4f207d6d..593302ef 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -201,14 +201,13 @@ i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT); if (ret) goto fail; - + obj_priv = obj->driver_private; offset = obj_priv->gtt_offset + args->offset; obj_priv->dirty = 1; - + while (remain > 0) { - - /** Operation in this page + /* Operation in this page * * i = page number * o = offset within page @@ -221,7 +220,7 @@ i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj, l = PAGE_SIZE - o; pfn = (dev->agp->base >> PAGE_SHIFT) + i; - + #ifdef DRM_KMAP_ATOMIC_PROT_PFN /* kmap_atomic can't map IO pages on non-HIGHMEM kernels */ @@ -231,7 +230,8 @@ i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj, DRM_INFO("pwrite i %d o %d l %d pfn %ld vaddr %p\n", i, o, l, pfn, vaddr); #endif - unwritten = __copy_from_user_inatomic_nocache(vaddr + o, user_data, l); + unwritten = __copy_from_user_inatomic_nocache(vaddr + o, + user_data, l); kunmap_atomic(vaddr, KM_USER0); if (unwritten) @@ -239,7 +239,8 @@ i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj, { vaddr = ioremap(pfn << PAGE_SHIFT, PAGE_SIZE); #if WATCH_PWRITE - DRM_INFO("pwrite slow i %d o %d l %d pfn %ld vaddr %p\n", + DRM_INFO("pwrite slow i %d o %d l %d " + "pfn %ld vaddr %p\n", i, o, l, pfn, vaddr); #endif if (vaddr == NULL) { @@ -268,7 +269,7 @@ i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj, #endif fail: - i915_gem_object_unpin (obj); + i915_gem_object_unpin(obj); mutex_unlock(&dev->struct_mutex); return ret; @@ -329,11 +330,11 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, return -EBADF; obj_priv = obj->driver_private; - /** Bounds check destination. + /* Bounds check destination. * * XXX: This could use review for overflow issues... */ - if (args->offset > obj->size || args->size > obj->size || + if (args->offset > obj->size || args->size > obj->size || args->offset + args->size > obj->size) { drm_gem_object_unreference(obj); return -EINVAL; @@ -413,14 +414,13 @@ i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, DRM_INFO("%s: sw_finish %d (%p)\n", __func__, args->handle, obj); #endif - obj_priv = obj->driver_private; - - /** Pinned buffers may be scanout, so flush the cache - */ - if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) { - i915_gem_clflush_object(obj); - drm_agp_chipset_flush(dev); - } + obj_priv = obj->driver_private; + + /* Pinned buffers may be scanout, so flush the cache */ + if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) { + i915_gem_clflush_object(obj); + drm_agp_chipset_flush(dev); + } drm_gem_object_unreference(obj); mutex_unlock(&dev->struct_mutex); return ret; @@ -577,7 +577,7 @@ i915_add_request(struct drm_device *dev, uint32_t flush_domains) list_add_tail(&request->list, &dev_priv->mm.request_list); if (was_empty) - schedule_delayed_work (&dev_priv->mm.retire_work, HZ); + schedule_delayed_work(&dev_priv->mm.retire_work, HZ); return seqno; } @@ -587,7 +587,6 @@ i915_add_request(struct drm_device *dev, uint32_t flush_domains) * Ensures that all commands in the ring are finished * before signalling the CPU */ - uint32_t i915_retire_commands(struct drm_device *dev) { @@ -704,7 +703,8 @@ i915_gem_retire_requests(struct drm_device *dev) list); retiring_seqno = request->seqno; - if (i915_seqno_passed(seqno, retiring_seqno) || dev_priv->mm.wedged) { + if (i915_seqno_passed(seqno, retiring_seqno) || + dev_priv->mm.wedged) { i915_gem_retire_request(dev, request); list_del(&request->list); @@ -727,7 +727,7 @@ i915_gem_retire_work_handler(struct work_struct *work) mutex_lock(&dev->struct_mutex); i915_gem_retire_requests(dev); if (!list_empty(&dev_priv->mm.request_list)) - schedule_delayed_work (&dev_priv->mm.retire_work, HZ); + schedule_delayed_work(&dev_priv->mm.retire_work, HZ); mutex_unlock(&dev->struct_mutex); } @@ -748,7 +748,8 @@ i915_wait_request(struct drm_device *dev, uint32_t seqno) i915_user_irq_on(dev_priv); ret = wait_event_interruptible(dev_priv->irq_queue, i915_seqno_passed(i915_get_gem_seqno(dev), - seqno) || dev_priv->mm.wedged); + seqno) || + dev_priv->mm.wedged); i915_user_irq_off(dev_priv); dev_priv->mm.waiting_gem_seqno = 0; } @@ -787,7 +788,8 @@ i915_gem_flush(struct drm_device *dev, if (flush_domains & I915_GEM_DOMAIN_CPU) drm_agp_chipset_flush(dev); - if ((invalidate_domains|flush_domains) & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) { + if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU | + I915_GEM_DOMAIN_GTT)) { /* * read/write caches: * @@ -914,7 +916,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) */ ret = i915_gem_object_wait_rendering(obj); if (ret) { - DRM_ERROR ("wait_rendering failed: %d\n", ret); + DRM_ERROR("wait_rendering failed: %d\n", ret); return ret; } @@ -944,7 +946,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) if (obj_priv->gtt_space) { atomic_dec(&dev->gtt_count); atomic_sub(obj->size, &dev->gtt_memory); - + drm_memrange_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; } @@ -1029,7 +1031,8 @@ i915_gem_evict_something(struct drm_device *dev) continue; } - DRM_ERROR("inactive empty %d request empty %d flushing empty %d\n", + DRM_ERROR("inactive empty %d request empty %d " + "flushing empty %d\n", list_empty(&dev_priv->mm.inactive_list), list_empty(&dev_priv->mm.request_list), list_empty(&dev_priv->mm.flushing_list)); @@ -1050,7 +1053,7 @@ i915_gem_object_get_page_list(struct drm_gem_object *obj) struct inode *inode; struct page *page; int ret; - + if (obj_priv->page_list) return 0; @@ -1076,7 +1079,7 @@ i915_gem_object_get_page_list(struct drm_gem_object *obj) page = NULL; } ret = shmem_getpage(inode, i, &page, SGP_DIRTY, NULL); - + if (ret) { DRM_ERROR("shmem_getpage failed: %d\n", ret); i915_gem_object_free_page_list(obj); @@ -1320,8 +1323,8 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, #if WATCH_BUF DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n", - __func__, obj, - obj->read_domains, read_domains, + __func__, obj, + obj->read_domains, read_domains, obj->write_domain, write_domain); #endif /* @@ -1359,7 +1362,8 @@ i915_gem_object_set_domain(struct drm_gem_object *obj, * flushed before the cpu cache is invalidated */ if ((invalidate_domains & I915_GEM_DOMAIN_CPU) && - (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))) { + (flush_domains & ~(I915_GEM_DOMAIN_CPU | + I915_GEM_DOMAIN_GTT))) { ret = i915_gem_object_wait_rendering(obj); if (ret) return ret; @@ -1741,7 +1745,8 @@ i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv) mutex_lock(&dev->struct_mutex); seqno = i915_file_priv->mm.last_gem_throttle_seqno; - i915_file_priv->mm.last_gem_throttle_seqno = i915_file_priv->mm.last_gem_seqno; + i915_file_priv->mm.last_gem_throttle_seqno = + i915_file_priv->mm.last_gem_seqno; if (seqno) ret = i915_wait_request(dev, seqno); mutex_unlock(&dev->struct_mutex); @@ -1798,7 +1803,7 @@ i915_gem_execbuffer(struct drm_device *dev, void *data, mutex_unlock(&dev->struct_mutex); return -EIO; } - + if (dev_priv->mm.suspended) { DRM_ERROR("Execbuf while VT-switched.\n"); mutex_unlock(&dev->struct_mutex); @@ -1979,7 +1984,9 @@ i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) if (obj_priv->pin_count == 1) { atomic_inc(&dev->pin_count); atomic_add(obj->size, &dev->pin_memory); - if (!obj_priv->active && (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) == 0 && + if (!obj_priv->active && + (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | + I915_GEM_DOMAIN_GTT)) == 0 && !list_empty(&obj_priv->list)) list_del_init(&obj_priv->list); } @@ -2005,7 +2012,9 @@ i915_gem_object_unpin(struct drm_gem_object *obj) * the inactive list */ if (obj_priv->pin_count == 0) { - if (!obj_priv->active && (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) == 0) + if (!obj_priv->active && + (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | + I915_GEM_DOMAIN_GTT)) == 0) list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list); atomic_dec(&dev->pin_count); @@ -2041,7 +2050,7 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data, return ret; } - /** XXX - flush the CPU caches for pinned objects + /* XXX - flush the CPU caches for pinned objects * as the X server doesn't manage domains yet */ if (obj->write_domain & I915_GEM_DOMAIN_CPU) { @@ -2164,7 +2173,7 @@ i915_gem_set_domain(struct drm_gem_object *obj, if (ret) return ret; flush_domains = i915_gem_dev_set_domain(obj->dev); - + if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) (void) i915_add_request(dev, flush_domains); @@ -2225,7 +2234,8 @@ i915_gem_idle(struct drm_device *dev) */ i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT), ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); - seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); + seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU | + I915_GEM_DOMAIN_GTT)); if (seqno == 0) { mutex_unlock(&dev->struct_mutex); @@ -2468,10 +2478,10 @@ i915_gem_lastclose(struct drm_device *dev) ret = i915_gem_idle(dev); if (ret) DRM_ERROR("failed to idle hardware: %d\n", ret); - + i915_gem_cleanup_ringbuffer(dev); } - + mutex_unlock(&dev->struct_mutex); } diff --git a/linux-core/i915_gem_proc.c b/linux-core/i915_gem_proc.c index 41060a4b..90351ac7 100644 --- a/linux-core/i915_gem_proc.c +++ b/linux-core/i915_gem_proc.c @@ -35,7 +35,7 @@ static int i915_gem_active_info(char *buf, char **start, off_t offset, int request, int *eof, void *data) { - struct drm_minor *minor = (struct drm_minor *) data; + struct drm_minor *minor = (struct drm_minor *) data; struct drm_device *dev = minor->dev; drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv; @@ -74,7 +74,7 @@ static int i915_gem_active_info(char *buf, char **start, off_t offset, static int i915_gem_flushing_info(char *buf, char **start, off_t offset, int request, int *eof, void *data) { - struct drm_minor *minor = (struct drm_minor *) data; + struct drm_minor *minor = (struct drm_minor *) data; struct drm_device *dev = minor->dev; drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv; @@ -112,7 +112,7 @@ static int i915_gem_flushing_info(char *buf, char **start, off_t offset, static int i915_gem_inactive_info(char *buf, char **start, off_t offset, int request, int *eof, void *data) { - struct drm_minor *minor = (struct drm_minor *) data; + struct drm_minor *minor = (struct drm_minor *) data; struct drm_device *dev = minor->dev; drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv; @@ -150,7 +150,7 @@ static int i915_gem_inactive_info(char *buf, char **start, off_t offset, static int i915_gem_request_info(char *buf, char **start, off_t offset, int request, int *eof, void *data) { - struct drm_minor *minor = (struct drm_minor *) data; + struct drm_minor *minor = (struct drm_minor *) data; struct drm_device *dev = minor->dev; drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_request *gem_request; @@ -167,10 +167,10 @@ static int i915_gem_request_info(char *buf, char **start, off_t offset, list_for_each_entry(gem_request, &dev_priv->mm.request_list, list) { - DRM_PROC_PRINT (" %d @ %d %08x\n", - gem_request->seqno, - (int) (jiffies - gem_request->emitted_jiffies), - gem_request->flush_domains); + DRM_PROC_PRINT(" %d @ %d %08x\n", + gem_request->seqno, + (int) (jiffies - gem_request->emitted_jiffies), + gem_request->flush_domains); } if (len > request + offset) return request; @@ -181,7 +181,7 @@ static int i915_gem_request_info(char *buf, char **start, off_t offset, static int i915_gem_seqno_info(char *buf, char **start, off_t offset, int request, int *eof, void *data) { - struct drm_minor *minor = (struct drm_minor *) data; + struct drm_minor *minor = (struct drm_minor *) data; struct drm_device *dev = minor->dev; drm_i915_private_t *dev_priv = dev->dev_private; int len = 0; @@ -194,7 +194,8 @@ static int i915_gem_seqno_info(char *buf, char **start, off_t offset, *start = &buf[offset]; *eof = 0; DRM_PROC_PRINT("Current sequence: %d\n", i915_get_gem_seqno(dev)); - DRM_PROC_PRINT("Waiter sequence: %d\n", dev_priv->mm.waiting_gem_seqno); + DRM_PROC_PRINT("Waiter sequence: %d\n", + dev_priv->mm.waiting_gem_seqno); DRM_PROC_PRINT("IRQ sequence: %d\n", dev_priv->mm.irq_gem_seqno); if (len > request + offset) return request; @@ -206,7 +207,7 @@ static int i915_gem_seqno_info(char *buf, char **start, off_t offset, static int i915_interrupt_info(char *buf, char **start, off_t offset, int request, int *eof, void *data) { - struct drm_minor *minor = (struct drm_minor *) data; + struct drm_minor *minor = (struct drm_minor *) data; struct drm_device *dev = minor->dev; drm_i915_private_t *dev_priv = dev->dev_private; int len = 0; @@ -243,8 +244,10 @@ static int i915_interrupt_info(char *buf, char **start, off_t offset, } static struct drm_proc_list { - const char *name; /**< file name */ - int (*f) (char *, char **, off_t, int, int *, void *); /**< proc callback*/ + /** file name */ + const char *name; + /** proc callback*/ + int (*f) (char *, char **, off_t, int, int *, void *); } i915_gem_proc_list[] = { {"i915_gem_active", i915_gem_active_info}, {"i915_gem_flushing", i915_gem_flushing_info}, diff --git a/linux-core/i915_gem_tiling.c b/linux-core/i915_gem_tiling.c index 2841ff1f..90029192 100644 --- a/linux-core/i915_gem_tiling.c +++ b/linux-core/i915_gem_tiling.c @@ -91,10 +91,10 @@ i915_gem_detect_bit_6_swizzle(struct drm_device *dev) uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN; uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN; int mchbar_offset; - volatile char *mchbar; + char __iomem *mchbar; int ret; - bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0,0)); + bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0)); if (bridge == NULL) { DRM_ERROR("Couldn't get bridge device\n"); return; @@ -165,7 +165,7 @@ i915_gem_detect_bit_6_swizzle(struct drm_device *dev) swizzle_x = I915_BIT_6_SWIZZLE_9_10; swizzle_y = I915_BIT_6_SWIZZLE_9; } - } else if (IS_I9XX(dev)){ + } else if (IS_I9XX(dev)) { uint32_t dcc; /* On 915-945 and GM965, channel interleave by the CPU is -- cgit v1.2.3 From e68b57c17da422f61b34c8221c8f4655c676b925 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 28 Jul 2008 16:33:50 -0700 Subject: intel-gem: checkpatch.pl on drm_proc.c new contents. --- linux-core/drm_proc.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drm_proc.c b/linux-core/drm_proc.c index 2bbe7eea..d3845bb3 100644 --- a/linux-core/drm_proc.c +++ b/linux-core/drm_proc.c @@ -594,20 +594,20 @@ struct drm_gem_name_info_data { int eof; }; -static int drm_gem_one_name_info (int id, void *ptr, void *data) +static int drm_gem_one_name_info(int id, void *ptr, void *data) { struct drm_gem_object *obj = ptr; struct drm_gem_name_info_data *nid = data; - DRM_INFO ("name %d size %d\n", obj->name, obj->size); + DRM_INFO("name %d size %d\n", obj->name, obj->size); if (nid->eof) return 0; - - nid->len += sprintf (&nid->buf[nid->len], - "%6d%9d%8d%9d\n", - obj->name, obj->size, - atomic_read(&obj->handlecount.refcount), - atomic_read(&obj->refcount.refcount)); + + nid->len += sprintf(&nid->buf[nid->len], + "%6d%9d%8d%9d\n", + obj->name, obj->size, + atomic_read(&obj->handlecount.refcount), + atomic_read(&obj->refcount.refcount)); if (nid->len > DRM_PROC_LIMIT) { nid->eof = 1; return 0; @@ -618,20 +618,20 @@ static int drm_gem_one_name_info (int id, void *ptr, void *data) static int drm_gem_name_info(char *buf, char **start, off_t offset, int request, int *eof, void *data) { - struct drm_minor *minor = (struct drm_minor *) data; + struct drm_minor *minor = (struct drm_minor *) data; struct drm_device *dev = minor->dev; struct drm_gem_name_info_data nid; - + if (offset > DRM_PROC_LIMIT) { *eof = 1; return 0; } - nid.len = sprintf (buf, " name size handles refcount\n"); + nid.len = sprintf(buf, " name size handles refcount\n"); nid.buf = buf; nid.eof = 0; - idr_for_each (&dev->object_name_idr, drm_gem_one_name_info, &nid); - + idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, &nid); + *start = &buf[offset]; *eof = 0; if (nid.len > request + offset) @@ -643,10 +643,10 @@ static int drm_gem_name_info(char *buf, char **start, off_t offset, static int drm_gem_object_info(char *buf, char **start, off_t offset, int request, int *eof, void *data) { - struct drm_minor *minor = (struct drm_minor *) data; + struct drm_minor *minor = (struct drm_minor *) data; struct drm_device *dev = minor->dev; int len = 0; - + if (offset > DRM_PROC_LIMIT) { *eof = 1; return 0; @@ -654,11 +654,11 @@ static int drm_gem_object_info(char *buf, char **start, off_t offset, *start = &buf[offset]; *eof = 0; - DRM_PROC_PRINT("%d objects\n", atomic_read (&dev->object_count)); - DRM_PROC_PRINT("%d object bytes\n", atomic_read (&dev->object_memory)); - DRM_PROC_PRINT("%d pinned\n", atomic_read (&dev->pin_count)); - DRM_PROC_PRINT("%d pin bytes\n", atomic_read (&dev->pin_memory)); - DRM_PROC_PRINT("%d gtt bytes\n", atomic_read (&dev->gtt_memory)); + DRM_PROC_PRINT("%d objects\n", atomic_read(&dev->object_count)); + DRM_PROC_PRINT("%d object bytes\n", atomic_read(&dev->object_memory)); + DRM_PROC_PRINT("%d pinned\n", atomic_read(&dev->pin_count)); + DRM_PROC_PRINT("%d pin bytes\n", atomic_read(&dev->pin_memory)); + DRM_PROC_PRINT("%d gtt bytes\n", atomic_read(&dev->gtt_memory)); DRM_PROC_PRINT("%d gtt total\n", dev->gtt_total); if (len > request + offset) return request; -- cgit v1.2.3 From 0f78e30e0a189b76df3558f53ff2a92b841cd7ab Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 29 Jul 2008 11:10:47 -0700 Subject: intel-gem: Check return value of pci_read_base. --- linux-core/i915_gem_tiling.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem_tiling.c b/linux-core/i915_gem_tiling.c index 90029192..a4ff736f 100644 --- a/linux-core/i915_gem_tiling.c +++ b/linux-core/i915_gem_tiling.c @@ -115,7 +115,11 @@ i915_gem_detect_bit_6_swizzle(struct drm_device *dev) * since the bridge would only ever use standard BARs 0-1 (though it * doesn't anyway) */ - pci_read_base(bridge, mchbar_offset, &bridge->resource[2]); + ret = pci_read_base(bridge, mchbar_offset, &bridge->resource[2]); + if (ret != 0) { + DRM_ERROR("pci_read_base failed: %d\n", ret); + return; + } mchbar = ioremap(pci_resource_start(bridge, 2), pci_resource_len(bridge, 2)); -- cgit v1.2.3 From 8fc72aef70cdec71e3dbce548046974aecc15325 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 29 Jul 2008 20:26:38 -0700 Subject: intel-gem: actually add i915_gem_debug.c. --- linux-core/i915_gem_debug.c | 202 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 linux-core/i915_gem_debug.c (limited to 'linux-core') diff --git a/linux-core/i915_gem_debug.c b/linux-core/i915_gem_debug.c new file mode 100644 index 00000000..a2d6f289 --- /dev/null +++ b/linux-core/i915_gem_debug.c @@ -0,0 +1,202 @@ +/* + * Copyright © 2008 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Keith Packard + * + */ + +#include "drmP.h" +#include "drm.h" +#include "drm_compat.h" +#include "i915_drm.h" +#include "i915_drv.h" + +#if WATCH_INACTIVE +void +i915_verify_inactive(struct drm_device *dev, char *file, int line) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_gem_object *obj; + struct drm_i915_gem_object *obj_priv; + + list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { + obj = obj_priv->obj; + if (obj_priv->pin_count || obj_priv->active || + (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | + I915_GEM_DOMAIN_GTT))) + DRM_ERROR("inactive %p (p %d a %d w %x) %s:%d\n", + obj, + obj_priv->pin_count, obj_priv->active, + obj->write_domain, file, line); + } +} +#endif /* WATCH_INACTIVE */ + + +#if WATCH_BUF | WATCH_EXEC | WATCH_PWRITE +static void +i915_gem_dump_page(struct page *page, uint32_t start, uint32_t end, + uint32_t bias, uint32_t mark) +{ + uint32_t *mem = kmap_atomic(page, KM_USER0); + int i; + for (i = start; i < end; i += 4) + DRM_INFO("%08x: %08x%s\n", + (int) (bias + i), mem[i / 4], + (bias + i == mark) ? " ********" : ""); + kunmap_atomic(mem, KM_USER0); + /* give syslog time to catch up */ + msleep(1); +} + +void +i915_gem_dump_object(struct drm_gem_object *obj, int len, + const char *where, uint32_t mark) +{ + struct drm_i915_gem_object *obj_priv = obj->driver_private; + int page; + + DRM_INFO("%s: object at offset %08x\n", where, obj_priv->gtt_offset); + for (page = 0; page < (len + PAGE_SIZE-1) / PAGE_SIZE; page++) { + int page_len, chunk, chunk_len; + + page_len = len - page * PAGE_SIZE; + if (page_len > PAGE_SIZE) + page_len = PAGE_SIZE; + + for (chunk = 0; chunk < page_len; chunk += 128) { + chunk_len = page_len - chunk; + if (chunk_len > 128) + chunk_len = 128; + i915_gem_dump_page(obj_priv->page_list[page], + chunk, chunk + chunk_len, + obj_priv->gtt_offset + + page * PAGE_SIZE, + mark); + } + } +} +#endif + +#if WATCH_LRU +void +i915_dump_lru(struct drm_device *dev, const char *where) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + struct drm_i915_gem_object *obj_priv; + + DRM_INFO("active list %s {\n", where); + list_for_each_entry(obj_priv, &dev_priv->mm.active_list, + list) + { + DRM_INFO(" %p: %08x\n", obj_priv, + obj_priv->last_rendering_seqno); + } + DRM_INFO("}\n"); + DRM_INFO("flushing list %s {\n", where); + list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, + list) + { + DRM_INFO(" %p: %08x\n", obj_priv, + obj_priv->last_rendering_seqno); + } + DRM_INFO("}\n"); + DRM_INFO("inactive %s {\n", where); + list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) { + DRM_INFO(" %p: %08x\n", obj_priv, + obj_priv->last_rendering_seqno); + } + DRM_INFO("}\n"); +} +#endif + + +#if WATCH_COHERENCY +void +i915_gem_object_check_coherency(struct drm_gem_object *obj, int handle) +{ + struct drm_device *dev = obj->dev; + struct drm_i915_gem_object *obj_priv = obj->driver_private; + int page; + uint32_t *gtt_mapping; + uint32_t *backing_map = NULL; + int bad_count = 0; + + DRM_INFO("%s: checking coherency of object %p@0x%08x (%d, %dkb):\n", + __func__, obj, obj_priv->gtt_offset, handle, + obj->size / 1024); + + gtt_mapping = ioremap(dev->agp->base + obj_priv->gtt_offset, + obj->size); + if (gtt_mapping == NULL) { + DRM_ERROR("failed to map GTT space\n"); + return; + } + + for (page = 0; page < obj->size / PAGE_SIZE; page++) { + int i; + + backing_map = kmap_atomic(obj_priv->page_list[page], KM_USER0); + + if (backing_map == NULL) { + DRM_ERROR("failed to map backing page\n"); + goto out; + } + + for (i = 0; i < PAGE_SIZE / 4; i++) { + uint32_t cpuval = backing_map[i]; + uint32_t gttval = readl(gtt_mapping + + page * 1024 + i); + + if (cpuval != gttval) { + DRM_INFO("incoherent CPU vs GPU at 0x%08x: " + "0x%08x vs 0x%08x\n", + (int)(obj_priv->gtt_offset + + page * PAGE_SIZE + i * 4), + cpuval, gttval); + if (bad_count++ >= 8) { + DRM_INFO("...\n"); + goto out; + } + } + } + kunmap_atomic(backing_map, KM_USER0); + backing_map = NULL; + } + + out: + if (backing_map != NULL) + kunmap_atomic(backing_map, KM_USER0); + iounmap(gtt_mapping); + + /* give syslog time to catch up */ + msleep(1); + + /* Directly flush the object, since we just loaded values with the CPU + * from the backing pages and we don't want to disturb the cache + * management that we're trying to observe. + */ + + i915_gem_clflush_object(obj); +} +#endif -- cgit v1.2.3 From 33c8e03787308c2b86a4f724ba7ce4dc43918b6a Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 30 Jul 2008 11:22:48 -0700 Subject: Revert "Rename drm_mm.c and its fuctions to drm_memrange." This reverts commit 3ad8db2071d30c198403e605f2726fc5c3e46bfd. We ended up not needing that namespace, and I'd rather not have the churn for producing diffs. --- linux-core/Makefile.kernel | 2 +- linux-core/drmP.h | 43 +++---- linux-core/drm_bo.c | 38 +++--- linux-core/drm_bo_move.c | 2 +- linux-core/drm_drv.c | 2 +- linux-core/drm_memrange.c | 298 --------------------------------------------- linux-core/drm_mm.c | 298 +++++++++++++++++++++++++++++++++++++++++++++ linux-core/drm_objects.h | 6 +- linux-core/drm_sman.c | 22 ++-- linux-core/drm_sman.h | 4 +- linux-core/drm_stub.c | 6 +- linux-core/i915_gem.c | 20 ++- linux-core/nouveau_bo.c | 2 +- linux-core/nouveau_sgdma.c | 2 +- 14 files changed, 369 insertions(+), 376 deletions(-) delete mode 100644 linux-core/drm_memrange.c create mode 100644 linux-core/drm_mm.c (limited to 'linux-core') diff --git a/linux-core/Makefile.kernel b/linux-core/Makefile.kernel index f338b598..e5af2ec4 100644 --- a/linux-core/Makefile.kernel +++ b/linux-core/Makefile.kernel @@ -12,7 +12,7 @@ drm-objs := drm_auth.o drm_bufs.o drm_context.o drm_dma.o drm_drawable.o \ drm_lock.o drm_memory.o drm_proc.o drm_stub.o drm_vm.o \ drm_sysfs.o drm_pci.o drm_agpsupport.o drm_scatter.o \ drm_memory_debug.o ati_pcigart.o drm_sman.o \ - drm_hashtab.o drm_memrange.o drm_object.o drm_compat.o \ + drm_hashtab.o drm_mm.o drm_object.o drm_compat.o \ drm_fence.o drm_ttm.o drm_bo.o drm_bo_move.o drm_bo_lock.o \ drm_regman.o drm_vm_nopage_compat.o drm_gem.o tdfx-objs := tdfx_drv.o diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 6b5e1851..56522073 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -554,17 +554,17 @@ struct drm_sigdata { * Generic memory manager structs */ -struct drm_memrange_node { +struct drm_mm_node { struct list_head fl_entry; struct list_head ml_entry; int free; unsigned long start; unsigned long size; - struct drm_memrange *mm; + struct drm_mm *mm; void *private; }; -struct drm_memrange { +struct drm_mm { struct list_head fl_entry; struct list_head ml_entry; }; @@ -578,7 +578,7 @@ struct drm_map_list { struct drm_hash_item hash; struct drm_map *map; /**< mapping */ uint64_t user_token; - struct drm_memrange_node *file_offset_node; + struct drm_mm_node *file_offset_node; }; typedef struct drm_map drm_local_map_t; @@ -864,7 +864,7 @@ struct drm_device { struct list_head maplist; /**< Linked list of regions */ int map_count; /**< Number of mappable regions */ struct drm_open_hash map_hash; /**< User token hash table for maps */ - struct drm_memrange offset_manager; /**< User token manager */ + struct drm_mm offset_manager; /**< User token manager */ struct drm_open_hash object_hash; /**< User token hash table for objects */ struct address_space *dev_mapping; /**< For unmap_mapping_range() */ struct page *ttm_dummy_page; @@ -1336,27 +1336,22 @@ extern int drm_sysfs_device_add(struct drm_minor *minor); extern void drm_sysfs_device_remove(struct drm_minor *minor); /* - * Basic memory manager support (drm_memrange.c) + * Basic memory manager support (drm_mm.c) */ -extern struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * parent, - unsigned long size, - unsigned alignment); -extern void drm_memrange_put_block(struct drm_memrange_node *cur); -extern struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange *mm, - unsigned long size, - unsigned alignment, int best_match); -extern int drm_memrange_init(struct drm_memrange *mm, - unsigned long start, unsigned long size); -extern void drm_memrange_takedown(struct drm_memrange *mm); -extern int drm_memrange_clean(struct drm_memrange *mm); -extern unsigned long drm_memrange_tail_space(struct drm_memrange *mm); -extern int drm_memrange_remove_space_from_tail(struct drm_memrange *mm, - unsigned long size); -extern int drm_memrange_add_space_to_tail(struct drm_memrange *mm, - unsigned long size); - -static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block) +extern struct drm_mm_node * drm_mm_get_block(struct drm_mm_node * parent, unsigned long size, + unsigned alignment); +extern void drm_mm_put_block(struct drm_mm_node *cur); +extern struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm, unsigned long size, + unsigned alignment, int best_match); +extern int drm_mm_init(struct drm_mm *mm, unsigned long start, unsigned long size); +extern void drm_mm_takedown(struct drm_mm *mm); +extern int drm_mm_clean(struct drm_mm *mm); +extern unsigned long drm_mm_tail_space(struct drm_mm *mm); +extern int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size); +extern int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size); + +static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block) { return block->mm; } diff --git a/linux-core/drm_bo.c b/linux-core/drm_bo.c index 3abbb8c4..88b2ee66 100644 --- a/linux-core/drm_bo.c +++ b/linux-core/drm_bo.c @@ -418,14 +418,14 @@ static void drm_bo_cleanup_refs(struct drm_buffer_object *bo, int remove_all) if (!bo->fence) { list_del_init(&bo->lru); if (bo->mem.mm_node) { - drm_memrange_put_block(bo->mem.mm_node); + drm_mm_put_block(bo->mem.mm_node); if (bo->pinned_node == bo->mem.mm_node) bo->pinned_node = NULL; bo->mem.mm_node = NULL; } list_del_init(&bo->pinned_lru); if (bo->pinned_node) { - drm_memrange_put_block(bo->pinned_node); + drm_mm_put_block(bo->pinned_node); bo->pinned_node = NULL; } list_del_init(&bo->ddestroy); @@ -791,7 +791,7 @@ out: mutex_lock(&dev->struct_mutex); if (evict_mem.mm_node) { if (evict_mem.mm_node != bo->pinned_node) - drm_memrange_put_block(evict_mem.mm_node); + drm_mm_put_block(evict_mem.mm_node); evict_mem.mm_node = NULL; } drm_bo_add_to_lru(bo); @@ -810,7 +810,7 @@ static int drm_bo_mem_force_space(struct drm_device *dev, struct drm_bo_mem_reg *mem, uint32_t mem_type, int no_wait) { - struct drm_memrange_node *node; + struct drm_mm_node *node; struct drm_buffer_manager *bm = &dev->bm; struct drm_buffer_object *entry; struct drm_mem_type_manager *man = &bm->man[mem_type]; @@ -820,7 +820,7 @@ static int drm_bo_mem_force_space(struct drm_device *dev, mutex_lock(&dev->struct_mutex); do { - node = drm_memrange_search_free(&man->manager, num_pages, + node = drm_mm_search_free(&man->manager, num_pages, mem->page_alignment, 1); if (node) break; @@ -846,7 +846,7 @@ static int drm_bo_mem_force_space(struct drm_device *dev, return -ENOMEM; } - node = drm_memrange_get_block(node, num_pages, mem->page_alignment); + node = drm_mm_get_block(node, num_pages, mem->page_alignment); if (unlikely(!node)) { mutex_unlock(&dev->struct_mutex); return -ENOMEM; @@ -924,7 +924,7 @@ int drm_bo_mem_space(struct drm_buffer_object *bo, int type_found = 0; int type_ok = 0; int has_eagain = 0; - struct drm_memrange_node *node = NULL; + struct drm_mm_node *node = NULL; int ret; mem->mm_node = NULL; @@ -952,10 +952,10 @@ int drm_bo_mem_space(struct drm_buffer_object *bo, mutex_lock(&dev->struct_mutex); if (man->has_type && man->use_type) { type_found = 1; - node = drm_memrange_search_free(&man->manager, mem->num_pages, + node = drm_mm_search_free(&man->manager, mem->num_pages, mem->page_alignment, 1); if (node) - node = drm_memrange_get_block(node, mem->num_pages, + node = drm_mm_get_block(node, mem->num_pages, mem->page_alignment); } mutex_unlock(&dev->struct_mutex); @@ -1340,7 +1340,7 @@ out_unlock: if (ret || !move_unfenced) { if (mem.mm_node) { if (mem.mm_node != bo->pinned_node) - drm_memrange_put_block(mem.mm_node); + drm_mm_put_block(mem.mm_node); mem.mm_node = NULL; } drm_bo_add_to_lru(bo); @@ -1432,7 +1432,7 @@ static int drm_buffer_object_validate(struct drm_buffer_object *bo, if (bo->pinned_node != bo->mem.mm_node) { if (bo->pinned_node != NULL) - drm_memrange_put_block(bo->pinned_node); + drm_mm_put_block(bo->pinned_node); bo->pinned_node = bo->mem.mm_node; } @@ -1443,7 +1443,7 @@ static int drm_buffer_object_validate(struct drm_buffer_object *bo, mutex_lock(&dev->struct_mutex); if (bo->pinned_node != bo->mem.mm_node) - drm_memrange_put_block(bo->pinned_node); + drm_mm_put_block(bo->pinned_node); list_del_init(&bo->pinned_lru); bo->pinned_node = NULL; @@ -2082,7 +2082,7 @@ static int drm_bo_leave_list(struct drm_buffer_object *bo, if (bo->pinned_node == bo->mem.mm_node) bo->pinned_node = NULL; if (bo->pinned_node != NULL) { - drm_memrange_put_block(bo->pinned_node); + drm_mm_put_block(bo->pinned_node); bo->pinned_node = NULL; } mutex_unlock(&dev->struct_mutex); @@ -2223,8 +2223,8 @@ int drm_bo_clean_mm(struct drm_device *dev, unsigned mem_type, int kern_clean) drm_bo_force_list_clean(dev, &man->lru, mem_type, 1, 0, 0); drm_bo_force_list_clean(dev, &man->pinned, mem_type, 1, 0, 1); - if (drm_memrange_clean(&man->manager)) { - drm_memrange_takedown(&man->manager); + if (drm_mm_clean(&man->manager)) { + drm_mm_takedown(&man->manager); } else { ret = -EBUSY; } @@ -2295,7 +2295,7 @@ int drm_bo_init_mm(struct drm_device *dev, unsigned type, DRM_ERROR("Zero size memory manager type %d\n", type); return ret; } - ret = drm_memrange_init(&man->manager, p_offset, p_size); + ret = drm_mm_init(&man->manager, p_offset, p_size); if (ret) return ret; } @@ -2713,7 +2713,7 @@ static void drm_bo_takedown_vm_locked(struct drm_buffer_object *bo) list->user_token = 0; } if (list->file_offset_node) { - drm_memrange_put_block(list->file_offset_node); + drm_mm_put_block(list->file_offset_node); list->file_offset_node = NULL; } @@ -2756,7 +2756,7 @@ static int drm_bo_setup_vm_locked(struct drm_buffer_object *bo) atomic_inc(&bo->usage); map->handle = (void *)bo; - list->file_offset_node = drm_memrange_search_free(&dev->offset_manager, + list->file_offset_node = drm_mm_search_free(&dev->offset_manager, bo->mem.num_pages, 0, 0); if (unlikely(!list->file_offset_node)) { @@ -2764,7 +2764,7 @@ static int drm_bo_setup_vm_locked(struct drm_buffer_object *bo) return -ENOMEM; } - list->file_offset_node = drm_memrange_get_block(list->file_offset_node, + list->file_offset_node = drm_mm_get_block(list->file_offset_node, bo->mem.num_pages, 0); if (unlikely(!list->file_offset_node)) { diff --git a/linux-core/drm_bo_move.c b/linux-core/drm_bo_move.c index 850be5a3..bf0e1b74 100644 --- a/linux-core/drm_bo_move.c +++ b/linux-core/drm_bo_move.c @@ -41,7 +41,7 @@ static void drm_bo_free_old_node(struct drm_buffer_object *bo) if (old_mem->mm_node && (old_mem->mm_node != bo->pinned_node)) { mutex_lock(&bo->dev->struct_mutex); - drm_memrange_put_block(old_mem->mm_node); + drm_mm_put_block(old_mem->mm_node); mutex_unlock(&bo->dev->struct_mutex); } old_mem->mm_node = NULL; diff --git a/linux-core/drm_drv.c b/linux-core/drm_drv.c index c2445671..efd6416f 100644 --- a/linux-core/drm_drv.c +++ b/linux-core/drm_drv.c @@ -419,7 +419,7 @@ static void drm_cleanup(struct drm_device * dev) drm_ctxbitmap_cleanup(dev); drm_ht_remove(&dev->map_hash); - drm_memrange_takedown(&dev->offset_manager); + drm_mm_takedown(&dev->offset_manager); drm_ht_remove(&dev->object_hash); drm_put_minor(dev); diff --git a/linux-core/drm_memrange.c b/linux-core/drm_memrange.c deleted file mode 100644 index 7014c4e2..00000000 --- a/linux-core/drm_memrange.c +++ /dev/null @@ -1,298 +0,0 @@ -/************************************************************************** - * - * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL - * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR - * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE - * USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * - **************************************************************************/ - -/* - * Generic simple memory manager implementation. Intended to be used as a base - * class implementation for more advanced memory managers. - * - * Note that the algorithm used is quite simple and there might be substantial - * performance gains if a smarter free list is implemented. Currently it is just an - * unordered stack of free regions. This could easily be improved if an RB-tree - * is used instead. At least if we expect heavy fragmentation. - * - * Aligned allocations can also see improvement. - * - * Authors: - * Thomas Hellström - */ - -#include "drmP.h" -#include - -unsigned long drm_memrange_tail_space(struct drm_memrange *mm) -{ - struct list_head *tail_node; - struct drm_memrange_node *entry; - - tail_node = mm->ml_entry.prev; - entry = list_entry(tail_node, struct drm_memrange_node, ml_entry); - if (!entry->free) - return 0; - - return entry->size; -} - -int drm_memrange_remove_space_from_tail(struct drm_memrange *mm, unsigned long size) -{ - struct list_head *tail_node; - struct drm_memrange_node *entry; - - tail_node = mm->ml_entry.prev; - entry = list_entry(tail_node, struct drm_memrange_node, ml_entry); - if (!entry->free) - return -ENOMEM; - - if (entry->size <= size) - return -ENOMEM; - - entry->size -= size; - return 0; -} - - -static int drm_memrange_create_tail_node(struct drm_memrange *mm, - unsigned long start, - unsigned long size) -{ - struct drm_memrange_node *child; - - child = (struct drm_memrange_node *) - drm_ctl_alloc(sizeof(*child), DRM_MEM_MM); - if (!child) - return -ENOMEM; - - child->free = 1; - child->size = size; - child->start = start; - child->mm = mm; - - list_add_tail(&child->ml_entry, &mm->ml_entry); - list_add_tail(&child->fl_entry, &mm->fl_entry); - - return 0; -} - - -int drm_memrange_add_space_to_tail(struct drm_memrange *mm, unsigned long size) -{ - struct list_head *tail_node; - struct drm_memrange_node *entry; - - tail_node = mm->ml_entry.prev; - entry = list_entry(tail_node, struct drm_memrange_node, ml_entry); - if (!entry->free) { - return drm_memrange_create_tail_node(mm, entry->start + entry->size, size); - } - entry->size += size; - return 0; -} - -static struct drm_memrange_node *drm_memrange_split_at_start(struct drm_memrange_node *parent, - unsigned long size) -{ - struct drm_memrange_node *child; - - child = (struct drm_memrange_node *) - drm_ctl_alloc(sizeof(*child), DRM_MEM_MM); - if (!child) - return NULL; - - INIT_LIST_HEAD(&child->fl_entry); - - child->free = 0; - child->size = size; - child->start = parent->start; - child->mm = parent->mm; - - list_add_tail(&child->ml_entry, &parent->ml_entry); - INIT_LIST_HEAD(&child->fl_entry); - - parent->size -= size; - parent->start += size; - return child; -} - -struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * parent, - unsigned long size, unsigned alignment) -{ - - struct drm_memrange_node *align_splitoff = NULL; - struct drm_memrange_node *child; - unsigned tmp = 0; - - if (alignment) - tmp = parent->start % alignment; - - if (tmp) { - align_splitoff = drm_memrange_split_at_start(parent, alignment - tmp); - if (!align_splitoff) - return NULL; - } - - if (parent->size == size) { - list_del_init(&parent->fl_entry); - parent->free = 0; - return parent; - } else { - child = drm_memrange_split_at_start(parent, size); - } - - if (align_splitoff) - drm_memrange_put_block(align_splitoff); - - return child; -} -EXPORT_SYMBOL(drm_memrange_get_block); - -/* - * Put a block. Merge with the previous and / or next block if they are free. - * Otherwise add to the free stack. - */ - -void drm_memrange_put_block(struct drm_memrange_node * cur) -{ - - struct drm_memrange *mm = cur->mm; - struct list_head *cur_head = &cur->ml_entry; - struct list_head *root_head = &mm->ml_entry; - struct drm_memrange_node *prev_node = NULL; - struct drm_memrange_node *next_node; - - int merged = 0; - - if (cur_head->prev != root_head) { - prev_node = list_entry(cur_head->prev, struct drm_memrange_node, ml_entry); - if (prev_node->free) { - prev_node->size += cur->size; - merged = 1; - } - } - if (cur_head->next != root_head) { - next_node = list_entry(cur_head->next, struct drm_memrange_node, ml_entry); - if (next_node->free) { - if (merged) { - prev_node->size += next_node->size; - list_del(&next_node->ml_entry); - list_del(&next_node->fl_entry); - drm_ctl_free(next_node, sizeof(*next_node), - DRM_MEM_MM); - } else { - next_node->size += cur->size; - next_node->start = cur->start; - merged = 1; - } - } - } - if (!merged) { - cur->free = 1; - list_add(&cur->fl_entry, &mm->fl_entry); - } else { - list_del(&cur->ml_entry); - drm_ctl_free(cur, sizeof(*cur), DRM_MEM_MM); - } -} -EXPORT_SYMBOL(drm_memrange_put_block); - -struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange * mm, - unsigned long size, - unsigned alignment, int best_match) -{ - struct list_head *list; - const struct list_head *free_stack = &mm->fl_entry; - struct drm_memrange_node *entry; - struct drm_memrange_node *best; - unsigned long best_size; - unsigned wasted; - - best = NULL; - best_size = ~0UL; - - list_for_each(list, free_stack) { - entry = list_entry(list, struct drm_memrange_node, fl_entry); - wasted = 0; - - if (entry->size < size) - continue; - - if (alignment) { - register unsigned tmp = entry->start % alignment; - if (tmp) - wasted += alignment - tmp; - } - - - if (entry->size >= size + wasted) { - if (!best_match) - return entry; - if (size < best_size) { - best = entry; - best_size = entry->size; - } - } - } - - return best; -} -EXPORT_SYMBOL(drm_memrange_search_free); - -int drm_memrange_clean(struct drm_memrange * mm) -{ - struct list_head *head = &mm->ml_entry; - - return (head->next->next == head); -} - -int drm_memrange_init(struct drm_memrange * mm, unsigned long start, unsigned long size) -{ - INIT_LIST_HEAD(&mm->ml_entry); - INIT_LIST_HEAD(&mm->fl_entry); - - return drm_memrange_create_tail_node(mm, start, size); -} - -EXPORT_SYMBOL(drm_memrange_init); - -void drm_memrange_takedown(struct drm_memrange * mm) -{ - struct list_head *bnode = mm->fl_entry.next; - struct drm_memrange_node *entry; - - entry = list_entry(bnode, struct drm_memrange_node, fl_entry); - - if (entry->ml_entry.next != &mm->ml_entry || - entry->fl_entry.next != &mm->fl_entry) { - DRM_ERROR("Memory manager not clean. Delaying takedown\n"); - return; - } - - list_del(&entry->fl_entry); - list_del(&entry->ml_entry); - drm_ctl_free(entry, sizeof(*entry), DRM_MEM_MM); -} - -EXPORT_SYMBOL(drm_memrange_takedown); diff --git a/linux-core/drm_mm.c b/linux-core/drm_mm.c new file mode 100644 index 00000000..1de7059a --- /dev/null +++ b/linux-core/drm_mm.c @@ -0,0 +1,298 @@ +/************************************************************************** + * + * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * + **************************************************************************/ + +/* + * Generic simple memory manager implementation. Intended to be used as a base + * class implementation for more advanced memory managers. + * + * Note that the algorithm used is quite simple and there might be substantial + * performance gains if a smarter free list is implemented. Currently it is just an + * unordered stack of free regions. This could easily be improved if an RB-tree + * is used instead. At least if we expect heavy fragmentation. + * + * Aligned allocations can also see improvement. + * + * Authors: + * Thomas Hellström + */ + +#include "drmP.h" +#include + +unsigned long drm_mm_tail_space(struct drm_mm *mm) +{ + struct list_head *tail_node; + struct drm_mm_node *entry; + + tail_node = mm->ml_entry.prev; + entry = list_entry(tail_node, struct drm_mm_node, ml_entry); + if (!entry->free) + return 0; + + return entry->size; +} + +int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size) +{ + struct list_head *tail_node; + struct drm_mm_node *entry; + + tail_node = mm->ml_entry.prev; + entry = list_entry(tail_node, struct drm_mm_node, ml_entry); + if (!entry->free) + return -ENOMEM; + + if (entry->size <= size) + return -ENOMEM; + + entry->size -= size; + return 0; +} + + +static int drm_mm_create_tail_node(struct drm_mm *mm, + unsigned long start, + unsigned long size) +{ + struct drm_mm_node *child; + + child = (struct drm_mm_node *) + drm_ctl_alloc(sizeof(*child), DRM_MEM_MM); + if (!child) + return -ENOMEM; + + child->free = 1; + child->size = size; + child->start = start; + child->mm = mm; + + list_add_tail(&child->ml_entry, &mm->ml_entry); + list_add_tail(&child->fl_entry, &mm->fl_entry); + + return 0; +} + + +int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size) +{ + struct list_head *tail_node; + struct drm_mm_node *entry; + + tail_node = mm->ml_entry.prev; + entry = list_entry(tail_node, struct drm_mm_node, ml_entry); + if (!entry->free) { + return drm_mm_create_tail_node(mm, entry->start + entry->size, size); + } + entry->size += size; + return 0; +} + +static struct drm_mm_node *drm_mm_split_at_start(struct drm_mm_node *parent, + unsigned long size) +{ + struct drm_mm_node *child; + + child = (struct drm_mm_node *) + drm_ctl_alloc(sizeof(*child), DRM_MEM_MM); + if (!child) + return NULL; + + INIT_LIST_HEAD(&child->fl_entry); + + child->free = 0; + child->size = size; + child->start = parent->start; + child->mm = parent->mm; + + list_add_tail(&child->ml_entry, &parent->ml_entry); + INIT_LIST_HEAD(&child->fl_entry); + + parent->size -= size; + parent->start += size; + return child; +} + +struct drm_mm_node *drm_mm_get_block(struct drm_mm_node * parent, + unsigned long size, unsigned alignment) +{ + + struct drm_mm_node *align_splitoff = NULL; + struct drm_mm_node *child; + unsigned tmp = 0; + + if (alignment) + tmp = parent->start % alignment; + + if (tmp) { + align_splitoff = drm_mm_split_at_start(parent, alignment - tmp); + if (!align_splitoff) + return NULL; + } + + if (parent->size == size) { + list_del_init(&parent->fl_entry); + parent->free = 0; + return parent; + } else { + child = drm_mm_split_at_start(parent, size); + } + + if (align_splitoff) + drm_mm_put_block(align_splitoff); + + return child; +} +EXPORT_SYMBOL(drm_mm_get_block); + +/* + * Put a block. Merge with the previous and / or next block if they are free. + * Otherwise add to the free stack. + */ + +void drm_mm_put_block(struct drm_mm_node * cur) +{ + + struct drm_mm *mm = cur->mm; + struct list_head *cur_head = &cur->ml_entry; + struct list_head *root_head = &mm->ml_entry; + struct drm_mm_node *prev_node = NULL; + struct drm_mm_node *next_node; + + int merged = 0; + + if (cur_head->prev != root_head) { + prev_node = list_entry(cur_head->prev, struct drm_mm_node, ml_entry); + if (prev_node->free) { + prev_node->size += cur->size; + merged = 1; + } + } + if (cur_head->next != root_head) { + next_node = list_entry(cur_head->next, struct drm_mm_node, ml_entry); + if (next_node->free) { + if (merged) { + prev_node->size += next_node->size; + list_del(&next_node->ml_entry); + list_del(&next_node->fl_entry); + drm_ctl_free(next_node, sizeof(*next_node), + DRM_MEM_MM); + } else { + next_node->size += cur->size; + next_node->start = cur->start; + merged = 1; + } + } + } + if (!merged) { + cur->free = 1; + list_add(&cur->fl_entry, &mm->fl_entry); + } else { + list_del(&cur->ml_entry); + drm_ctl_free(cur, sizeof(*cur), DRM_MEM_MM); + } +} +EXPORT_SYMBOL(drm_mm_put_block); + +struct drm_mm_node *drm_mm_search_free(const struct drm_mm * mm, + unsigned long size, + unsigned alignment, int best_match) +{ + struct list_head *list; + const struct list_head *free_stack = &mm->fl_entry; + struct drm_mm_node *entry; + struct drm_mm_node *best; + unsigned long best_size; + unsigned wasted; + + best = NULL; + best_size = ~0UL; + + list_for_each(list, free_stack) { + entry = list_entry(list, struct drm_mm_node, fl_entry); + wasted = 0; + + if (entry->size < size) + continue; + + if (alignment) { + register unsigned tmp = entry->start % alignment; + if (tmp) + wasted += alignment - tmp; + } + + + if (entry->size >= size + wasted) { + if (!best_match) + return entry; + if (size < best_size) { + best = entry; + best_size = entry->size; + } + } + } + + return best; +} +EXPORT_SYMBOL(drm_mm_search_free); + +int drm_mm_clean(struct drm_mm * mm) +{ + struct list_head *head = &mm->ml_entry; + + return (head->next->next == head); +} + +int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size) +{ + INIT_LIST_HEAD(&mm->ml_entry); + INIT_LIST_HEAD(&mm->fl_entry); + + return drm_mm_create_tail_node(mm, start, size); +} + +EXPORT_SYMBOL(drm_mm_init); + +void drm_mm_takedown(struct drm_mm * mm) +{ + struct list_head *bnode = mm->fl_entry.next; + struct drm_mm_node *entry; + + entry = list_entry(bnode, struct drm_mm_node, fl_entry); + + if (entry->ml_entry.next != &mm->ml_entry || + entry->fl_entry.next != &mm->fl_entry) { + DRM_ERROR("Memory manager not clean. Delaying takedown\n"); + return; + } + + list_del(&entry->fl_entry); + list_del(&entry->ml_entry); + drm_ctl_free(entry, sizeof(*entry), DRM_MEM_MM); +} + +EXPORT_SYMBOL(drm_mm_takedown); diff --git a/linux-core/drm_objects.h b/linux-core/drm_objects.h index 6ec09ef8..6a900612 100644 --- a/linux-core/drm_objects.h +++ b/linux-core/drm_objects.h @@ -418,7 +418,7 @@ extern int drm_ttm_destroy(struct drm_ttm *ttm); */ struct drm_bo_mem_reg { - struct drm_memrange_node *mm_node; + struct drm_mm_node *mm_node; unsigned long size; unsigned long num_pages; uint32_t page_alignment; @@ -499,7 +499,7 @@ struct drm_buffer_object { unsigned long num_pages; /* For pinned buffers */ - struct drm_memrange_node *pinned_node; + struct drm_mm_node *pinned_node; uint32_t pinned_mem_type; struct list_head pinned_lru; @@ -534,7 +534,7 @@ struct drm_mem_type_manager { int has_type; int use_type; int kern_init_type; - struct drm_memrange manager; + struct drm_mm manager; struct list_head lru; struct list_head pinned; uint32_t flags; diff --git a/linux-core/drm_sman.c b/linux-core/drm_sman.c index 7c16f685..8421a939 100644 --- a/linux-core/drm_sman.c +++ b/linux-core/drm_sman.c @@ -88,34 +88,34 @@ EXPORT_SYMBOL(drm_sman_init); static void *drm_sman_mm_allocate(void *private, unsigned long size, unsigned alignment) { - struct drm_memrange *mm = (struct drm_memrange *) private; - struct drm_memrange_node *tmp; + struct drm_mm *mm = (struct drm_mm *) private; + struct drm_mm_node *tmp; - tmp = drm_memrange_search_free(mm, size, alignment, 1); + tmp = drm_mm_search_free(mm, size, alignment, 1); if (!tmp) { return NULL; } - tmp = drm_memrange_get_block(tmp, size, alignment); + tmp = drm_mm_get_block(tmp, size, alignment); return tmp; } static void drm_sman_mm_free(void *private, void *ref) { - struct drm_memrange_node *node = (struct drm_memrange_node *) ref; + struct drm_mm_node *node = (struct drm_mm_node *) ref; - drm_memrange_put_block(node); + drm_mm_put_block(node); } static void drm_sman_mm_destroy(void *private) { - struct drm_memrange *mm = (struct drm_memrange *) private; - drm_memrange_takedown(mm); + struct drm_mm *mm = (struct drm_mm *) private; + drm_mm_takedown(mm); drm_free(mm, sizeof(*mm), DRM_MEM_MM); } static unsigned long drm_sman_mm_offset(void *private, void *ref) { - struct drm_memrange_node *node = (struct drm_memrange_node *) ref; + struct drm_mm_node *node = (struct drm_mm_node *) ref; return node->start; } @@ -124,7 +124,7 @@ drm_sman_set_range(struct drm_sman * sman, unsigned int manager, unsigned long start, unsigned long size) { struct drm_sman_mm *sman_mm; - struct drm_memrange *mm; + struct drm_mm *mm; int ret; BUG_ON(manager >= sman->num_managers); @@ -135,7 +135,7 @@ drm_sman_set_range(struct drm_sman * sman, unsigned int manager, return -ENOMEM; } sman_mm->private = mm; - ret = drm_memrange_init(mm, start, size); + ret = drm_mm_init(mm, start, size); if (ret) { drm_free(mm, sizeof(*mm), DRM_MEM_MM); diff --git a/linux-core/drm_sman.h b/linux-core/drm_sman.h index 0299776c..39a39fef 100644 --- a/linux-core/drm_sman.h +++ b/linux-core/drm_sman.h @@ -45,7 +45,7 @@ /* * A class that is an abstration of a simple memory allocator. * The sman implementation provides a default such allocator - * using the drm_memrange.c implementation. But the user can replace it. + * using the drm_mm.c implementation. But the user can replace it. * See the SiS implementation, which may use the SiS FB kernel module * for memory management. */ @@ -116,7 +116,7 @@ extern int drm_sman_init(struct drm_sman * sman, unsigned int num_managers, unsigned int user_order, unsigned int owner_order); /* - * Initialize a drm_memrange.c allocator. Should be called only once for each + * Initialize a drm_mm.c allocator. Should be called only once for each * manager unless a customized allogator is used. */ diff --git a/linux-core/drm_stub.c b/linux-core/drm_stub.c index 1aacd4ff..1518bc6d 100644 --- a/linux-core/drm_stub.c +++ b/linux-core/drm_stub.c @@ -115,15 +115,15 @@ static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev, if (drm_ht_create(&dev->map_hash, DRM_MAP_HASH_ORDER)) { return -ENOMEM; } - if (drm_memrange_init(&dev->offset_manager, DRM_FILE_PAGE_OFFSET_START, - DRM_FILE_PAGE_OFFSET_SIZE)) { + if (drm_mm_init(&dev->offset_manager, DRM_FILE_PAGE_OFFSET_START, + DRM_FILE_PAGE_OFFSET_SIZE)) { drm_ht_remove(&dev->map_hash); return -ENOMEM; } if (drm_ht_create(&dev->object_hash, DRM_OBJECT_HASH_ORDER)) { drm_ht_remove(&dev->map_hash); - drm_memrange_takedown(&dev->offset_manager); + drm_mm_takedown(&dev->offset_manager); return -ENOMEM; } diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 4c167d29..70f11515 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -67,7 +67,7 @@ i915_gem_init_ioctl(struct drm_device *dev, void *data, return -EINVAL; } - drm_memrange_init(&dev_priv->mm.gtt_space, args->gtt_start, + drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start, args->gtt_end - args->gtt_start); dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start); @@ -947,7 +947,7 @@ i915_gem_object_unbind(struct drm_gem_object *obj) atomic_dec(&dev->gtt_count); atomic_sub(obj->size, &dev->gtt_memory); - drm_memrange_put_block(obj_priv->gtt_space); + drm_mm_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; } @@ -1101,7 +1101,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) struct drm_device *dev = obj->dev; drm_i915_private_t *dev_priv = dev->dev_private; struct drm_i915_gem_object *obj_priv = obj->driver_private; - struct drm_memrange_node *free_space; + struct drm_mm_node *free_space; int page_count, ret; if (alignment == 0) @@ -1112,13 +1112,11 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) } search_free: - free_space = drm_memrange_search_free(&dev_priv->mm.gtt_space, - obj->size, - alignment, 0); + free_space = drm_mm_search_free(&dev_priv->mm.gtt_space, + obj->size, alignment, 0); if (free_space != NULL) { - obj_priv->gtt_space = - drm_memrange_get_block(free_space, obj->size, - alignment); + obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size, + alignment); if (obj_priv->gtt_space != NULL) { obj_priv->gtt_space->private = obj; obj_priv->gtt_offset = obj_priv->gtt_space->start; @@ -1152,7 +1150,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) #endif ret = i915_gem_object_get_page_list(obj); if (ret) { - drm_memrange_put_block(obj_priv->gtt_space); + drm_mm_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; return ret; } @@ -1167,7 +1165,7 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) obj_priv->gtt_offset); if (obj_priv->agp_mem == NULL) { i915_gem_object_free_page_list(obj); - drm_memrange_put_block(obj_priv->gtt_space); + drm_mm_put_block(obj_priv->gtt_space); obj_priv->gtt_space = NULL; return -ENOMEM; } diff --git a/linux-core/nouveau_bo.c b/linux-core/nouveau_bo.c index 86347e03..ab3b23a4 100644 --- a/linux-core/nouveau_bo.c +++ b/linux-core/nouveau_bo.c @@ -229,7 +229,7 @@ out_cleanup: if (tmp_mem.mm_node) { mutex_lock(&dev->struct_mutex); if (tmp_mem.mm_node != bo->pinned_node) - drm_memrange_put_block(tmp_mem.mm_node); + drm_mm_put_block(tmp_mem.mm_node); tmp_mem.mm_node = NULL; mutex_unlock(&dev->struct_mutex); } diff --git a/linux-core/nouveau_sgdma.c b/linux-core/nouveau_sgdma.c index 81704ea1..cc4d5a92 100644 --- a/linux-core/nouveau_sgdma.c +++ b/linux-core/nouveau_sgdma.c @@ -280,7 +280,7 @@ nouveau_sgdma_nottm_hack_init(struct drm_device *dev) struct drm_nouveau_private *dev_priv = dev->dev_private; struct drm_ttm_backend *be; struct drm_scatter_gather sgreq; - struct drm_memrange_node mm_node; + struct drm_mm_node mm_node; struct drm_bo_mem_reg mem; int ret; -- cgit v1.2.3 From 689548b5270a0049cfaf844013ef61ad4c9eaeea Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 30 Jul 2008 11:29:32 -0700 Subject: intel-gem: Remove dead field from reverted changes. --- linux-core/drmP.h | 5 ----- 1 file changed, 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/drmP.h b/linux-core/drmP.h index 56522073..36a3d898 100644 --- a/linux-core/drmP.h +++ b/linux-core/drmP.h @@ -474,11 +474,6 @@ struct drm_lock_data { uint32_t kernel_waiters; uint32_t user_waiters; int idle_has_lock; - /** - * Boolean signaling that the lock is held on behalf of the - * file_priv client by the kernel in an ioctl handler. - */ - int kernel_held; }; /** -- cgit v1.2.3 From ceb3d5e3834452f9d54f974b8066f90168467443 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 5 Aug 2008 14:44:53 -0700 Subject: [gem-intel] Don't clear write_domain until flush completes In i915_gem_object_wait_rendering, if the object write domain is being written by the GPU, the appropriate flushing commands are written to the device and an additional request queued to mark that flush. Finally, the function blocks on that new request. The bug was that the write_domain in the object was cleared before the function blocked. If the wait is interrupted by a signal, the flushing commands may still be pending. With the current write_domain information lost, the restarted syscall will drop right through the write_domain test as that value was lost, and so the function will not block at all. Oops. Fixed by simply moving the write_domain clear until after the wait_request succeeds. Note that the restarted system call will generate an additional flush sequence and request, but that should be 'harmless', aside from a slight performance impact. Someday we'll track flushing more accurately and clear write_domains more efficiently, but for now, this should suffice. This bug was discovered in the 2d gem development by running x11perf -copypixwin500 and noticing that the window got cleared accidentally. --- linux-core/i915_gem.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 70f11515..acded2e8 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -381,6 +381,10 @@ i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, return -EBADF; mutex_lock(&dev->struct_mutex); +#if WATCH_BUF + DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n", + obj, obj->size, args->read_domains, args->write_domain); +#endif ret = i915_gem_set_domain(obj, file_priv, args->read_domains, args->write_domain); drm_gem_object_unreference(obj); @@ -411,8 +415,8 @@ i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, } #if WATCH_BUF - DRM_INFO("%s: sw_finish %d (%p)\n", - __func__, args->handle, obj); + DRM_INFO("%s: sw_finish %d (%p %d)\n", + __func__, args->handle, obj, obj->size); #endif obj_priv = obj->driver_private; @@ -853,18 +857,18 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) struct drm_device *dev = obj->dev; struct drm_i915_gem_object *obj_priv = obj->driver_private; int ret; + uint32_t write_domain; /* If there are writes queued to the buffer, flush and * create a new seqno to wait for. */ - if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) { - uint32_t write_domain = obj->write_domain; + write_domain = obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT); + if (write_domain) { #if WATCH_BUF DRM_INFO("%s: flushing object %p from write domain %08x\n", __func__, obj, write_domain); #endif i915_gem_flush(dev, 0, write_domain); - obj->write_domain = 0; i915_gem_object_move_to_active(obj); obj_priv->last_rendering_seqno = i915_add_request(dev, @@ -874,6 +878,7 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj); #endif } + /* If there is rendering queued on the buffer being evicted, wait for * it. */ @@ -885,6 +890,13 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) ret = i915_wait_request(dev, obj_priv->last_rendering_seqno); if (ret != 0) return ret; + if (write_domain) { +#if WATCH_BUF + DRM_INFO("%s: flushed object %p from write domain %08x\n", + __func__, obj, write_domain); +#endif + obj->write_domain = 0; + } } return 0; -- cgit v1.2.3 From dc0546c87ffc6701802d6141810c24954274e1ac Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 5 Aug 2008 16:06:40 -0700 Subject: [gem-intel] Retiring flush requests should clear flushed write_domains When i915_gem_retire_request has a flush which matches an object write domain, clear the write domain. This will move the object to the inactive list rather than the flushing list, avoiding trouble with objects left stuck on the flushing list. --- linux-core/i915_gem.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index acded2e8..354bd0db 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -661,6 +661,12 @@ i915_gem_retire_request(struct drm_device *dev, __func__, request->seqno, obj); #endif + /* If this request flushes the write domain, + * clear the write domain from the object now + */ + if (request->flush_domains & obj->write_domain) + obj->write_domain = 0; + if (obj->write_domain != 0) { list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list); @@ -760,7 +766,7 @@ i915_wait_request(struct drm_device *dev, uint32_t seqno) if (dev_priv->mm.wedged) ret = -EIO; - if (ret) + if (ret && ret != -ERESTARTSYS) DRM_ERROR("%s returns %d (awaiting %d at %d)\n", __func__, ret, seqno, i915_get_gem_seqno(dev)); @@ -890,13 +896,6 @@ i915_gem_object_wait_rendering(struct drm_gem_object *obj) ret = i915_wait_request(dev, obj_priv->last_rendering_seqno); if (ret != 0) return ret; - if (write_domain) { -#if WATCH_BUF - DRM_INFO("%s: flushed object %p from write domain %08x\n", - __func__, obj, write_domain); -#endif - obj->write_domain = 0; - } } return 0; -- cgit v1.2.3 From ac20e14d2361160cf199dc31c3fe1ffbacdf5bb7 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 4 Aug 2008 23:33:03 -0700 Subject: Switch from shmem_getpage to read_mapping_page --- linux-core/i915_gem.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'linux-core') diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c index 354bd0db..35dc5bd7 100644 --- a/linux-core/i915_gem.c +++ b/linux-core/i915_gem.c @@ -1083,20 +1083,12 @@ i915_gem_object_get_page_list(struct drm_gem_object *obj) inode = obj->filp->f_path.dentry->d_inode; mapping = inode->i_mapping; for (i = 0; i < page_count; i++) { - page = find_get_page(mapping, i); - if (page == NULL || !PageUptodate(page)) { - if (page) { - page_cache_release(page); - page = NULL; - } - ret = shmem_getpage(inode, i, &page, SGP_DIRTY, NULL); - - if (ret) { - DRM_ERROR("shmem_getpage failed: %d\n", ret); - i915_gem_object_free_page_list(obj); - return ret; - } - unlock_page(page); + page = read_mapping_page(mapping, i, NULL); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + DRM_ERROR("read_mapping_page failed: %d\n", ret); + i915_gem_object_free_page_list(obj); + return ret; } obj_priv->page_list[i] = page; } -- cgit v1.2.3