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(-) 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. --- .gitignore | 1 + linux-core/Makefile | 3 +- linux-core/Makefile.kernel | 3 +- linux-core/mmfs.h | 176 ++++++++++++++++++++++++++ linux-core/mmfs_drv.c | 299 +++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 4 +- tests/mmfs_basic.c | 68 +++++++++++ 7 files changed, 551 insertions(+), 3 deletions(-) create mode 100644 linux-core/mmfs.h create mode 100644 linux-core/mmfs_drv.c create mode 100644 tests/mmfs_basic.c diff --git a/.gitignore b/.gitignore index 0991da8c..fe64a289 100644 --- a/.gitignore +++ b/.gitignore @@ -58,6 +58,7 @@ tests/getclient tests/getstats tests/getversion tests/lock +tests/mmfs_basic tests/openclose tests/setversion tests/updatedraw 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"); diff --git a/tests/Makefile.am b/tests/Makefile.am index dce1754e..4a7b0119 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,5 @@ AM_CFLAGS = \ + -I $(top_srcdir)/linux-core \ -I $(top_srcdir)/shared-core \ -I $(top_srcdir)/libdrm @@ -22,7 +23,8 @@ TESTS = auth \ getstats \ lock \ setversion \ - updatedraw + updatedraw \ + mmfs_basic EXTRA_PROGRAMS = $(TESTS) CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LTLIBRARIES) diff --git a/tests/mmfs_basic.c b/tests/mmfs_basic.c new file mode 100644 index 00000000..907dc585 --- /dev/null +++ b/tests/mmfs_basic.c @@ -0,0 +1,68 @@ +/* + * 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 "mmfs.h" + +static void +create_mmfs_device() +{ + struct stat sb; + int ret; + + ret = stat(MMFS_DEVICE_PATH, &sb); + + if (ret == 0) + return; + + ret = mknod(MMFS_DEVICE_PATH, S_IFCHR | S_IRUSR | S_IWUSR, + makedev(MMFS_DEVICE_MAJOR, 0)); + + if (ret != 0) + errx(1, "mknod()"); +} + + +int main(int argc, char **argv) +{ + int fd; + + create_mmfs_device(); + + fd = open(MMFS_DEVICE_PATH, O_RDWR); + if (fd == -1) + errx(1, "open()"); + + close(fd); + + return 0; +} -- 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 ++++++++++++++++++ shared-core/mmfs.h | 139 +++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 1 - 4 files changed, 205 insertions(+), 177 deletions(-) mode change 100644 => 120000 linux-core/mmfs.h create mode 100644 linux-core/mmfs_drv.h create mode 100644 shared-core/mmfs.h 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); diff --git a/shared-core/mmfs.h b/shared-core/mmfs.h new file mode 100644 index 00000000..4cad3bdc --- /dev/null +++ b/shared-core/mmfs.h @@ -0,0 +1,139 @@ +/* + * 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 "mmfs_drv.h" +#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/tests/Makefile.am b/tests/Makefile.am index 4a7b0119..97752774 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,4 @@ AM_CFLAGS = \ - -I $(top_srcdir)/linux-core \ -I $(top_srcdir)/shared-core \ -I $(top_srcdir)/libdrm -- cgit v1.2.3 From c1fec43b553ea93460b58995a1229e84d8bb45b4 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 23 Apr 2008 11:32:31 -0700 Subject: Extend the mmfs basic test to do a couple of ioctls. --- shared-core/mmfs.h | 2 ++ tests/mmfs_basic.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/shared-core/mmfs.h b/shared-core/mmfs.h index 4cad3bdc..bc7e991f 100644 --- a/shared-core/mmfs.h +++ b/shared-core/mmfs.h @@ -25,6 +25,8 @@ * */ +#include + /** @file mmfs.h * This file provides ioctl and ioctl argument definitions for using the * mmfs device. diff --git a/tests/mmfs_basic.c b/tests/mmfs_basic.c index 907dc585..b0ae8905 100644 --- a/tests/mmfs_basic.c +++ b/tests/mmfs_basic.c @@ -27,9 +27,11 @@ #include #include +#include #include #include #include +#include #include #include "mmfs.h" @@ -51,6 +53,65 @@ create_mmfs_device() errx(1, "mknod()"); } +static void +test_bad_unref(int fd) +{ + struct mmfs_unreference_args unref; + int ret; + + printf("Testing error return on bad unreference ioctl.\n"); + + unref.handle = 0x10101010; + ret = ioctl(fd, MMFS_IOCTL_UNREFERENCE, &unref); + + assert(ret == -1 && errno == EINVAL); +} + +static void +test_bad_ioctl(int fd) +{ + int ret; + + printf("Testing error return on bad ioctl.\n"); + + ret = ioctl(fd, _IO(MMFS_IOCTL_BASE, 0xf0), 0); + + assert(ret == -1 && errno == EINVAL); +} + +static void +test_alloc_unref(int fd) +{ + struct mmfs_alloc_args alloc; + struct mmfs_unreference_args unref; + int ret; + + printf("Testing allocating and unreferencing an object.\n"); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = 16 * 1024; + ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc); + assert(ret == 0); + + unref.handle = alloc.handle; + ret = ioctl(fd, MMFS_IOCTL_UNREFERENCE, &unref); +} + +static void +test_alloc_close(int fd) +{ + struct mmfs_alloc_args alloc; + int ret; + + printf("Testing closing with an object allocated.\n"); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = 16 * 1024; + ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc); + assert(ret == 0); + + close(fd); +} int main(int argc, char **argv) { @@ -62,6 +123,11 @@ int main(int argc, char **argv) if (fd == -1) errx(1, "open()"); + test_bad_ioctl(fd); + test_bad_unref(fd); + test_alloc_unref(fd); + test_alloc_close(fd); + close(fd); return 0; -- 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. --- .gitignore | 1 + linux-core/mmfs_drv.c | 113 ++++++++++++++++++++++++++++++++++++++++++-- linux-core/mmfs_drv.h | 7 +-- shared-core/mmfs.h | 7 ++- tests/Makefile.am | 3 +- tests/drmtest.c | 33 +++++++++++++ tests/drmtest.h | 1 + tests/mmfs_basic.c | 24 +--------- tests/mmfs_readwrite.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 280 insertions(+), 34 deletions(-) create mode 100644 tests/mmfs_readwrite.c diff --git a/.gitignore b/.gitignore index fe64a289..5a4bcb13 100644 --- a/.gitignore +++ b/.gitignore @@ -59,6 +59,7 @@ tests/getstats tests/getversion tests/lock tests/mmfs_basic +tests/mmfs_readwrite tests/openclose tests/setversion tests/updatedraw 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); diff --git a/shared-core/mmfs.h b/shared-core/mmfs.h index bc7e991f..e0a4f25b 100644 --- a/shared-core/mmfs.h +++ b/shared-core/mmfs.h @@ -25,7 +25,12 @@ * */ -#include +#ifdef __linux__ +#include +#else +#include +#include +#endif /** @file mmfs.h * This file provides ioctl and ioctl argument definitions for using the diff --git a/tests/Makefile.am b/tests/Makefile.am index 97752774..e2931013 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -23,7 +23,8 @@ TESTS = auth \ lock \ setversion \ updatedraw \ - mmfs_basic + mmfs_basic \ + mmfs_readwrite EXTRA_PROGRAMS = $(TESTS) CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LTLIBRARIES) diff --git a/tests/drmtest.c b/tests/drmtest.c index cae99a0c..58f71a6a 100644 --- a/tests/drmtest.c +++ b/tests/drmtest.c @@ -26,7 +26,9 @@ */ #include +#include #include "drmtest.h" +#include "mmfs.h" /** Open the first DRM device we can find, searching up to 16 device nodes */ int drm_open_any(void) @@ -81,3 +83,34 @@ int drm_open_any_master(void) abort(); } +static void +create_mmfs_device() +{ + struct stat sb; + int ret; + + ret = stat(MMFS_DEVICE_PATH, &sb); + + if (ret == 0) + return; + + ret = mknod(MMFS_DEVICE_PATH, S_IFCHR | S_IRUSR | S_IWUSR, + makedev(MMFS_DEVICE_MAJOR, 0)); + + if (ret != 0) + errx(1, "mknod()"); +} + +int +open_mmfs_device() +{ + int fd; + + create_mmfs_device(); + + fd = open(MMFS_DEVICE_PATH, O_RDWR); + if (fd == -1) + errx(1, "open()"); + + return fd; +} diff --git a/tests/drmtest.h b/tests/drmtest.h index afa0df4a..b84ada71 100644 --- a/tests/drmtest.h +++ b/tests/drmtest.h @@ -35,3 +35,4 @@ int drm_open_any(void); int drm_open_any_master(void); +int open_mmfs_device(); diff --git a/tests/mmfs_basic.c b/tests/mmfs_basic.c index b0ae8905..c6975b0b 100644 --- a/tests/mmfs_basic.c +++ b/tests/mmfs_basic.c @@ -35,24 +35,6 @@ #include #include "mmfs.h" -static void -create_mmfs_device() -{ - struct stat sb; - int ret; - - ret = stat(MMFS_DEVICE_PATH, &sb); - - if (ret == 0) - return; - - ret = mknod(MMFS_DEVICE_PATH, S_IFCHR | S_IRUSR | S_IWUSR, - makedev(MMFS_DEVICE_MAJOR, 0)); - - if (ret != 0) - errx(1, "mknod()"); -} - static void test_bad_unref(int fd) { @@ -117,11 +99,7 @@ int main(int argc, char **argv) { int fd; - create_mmfs_device(); - - fd = open(MMFS_DEVICE_PATH, O_RDWR); - if (fd == -1) - errx(1, "open()"); + fd = open_mmfs_device(); test_bad_ioctl(fd); test_bad_unref(fd); diff --git a/tests/mmfs_readwrite.c b/tests/mmfs_readwrite.c new file mode 100644 index 00000000..09d1967d --- /dev/null +++ b/tests/mmfs_readwrite.c @@ -0,0 +1,125 @@ +/* + * 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 "mmfs.h" + +#define MMFS_BUFFER_SIZE 16384 + +int do_read(int fd, int handle, void *buf, int offset, int size) +{ + struct mmfs_pread_args read; + + /* Ensure that we don't have any convenient data in buf in case + * we fail. + */ + memset(buf, 0xd0, size); + + memset(&read, 0, sizeof(read)); + read.handle = handle; + read.data = buf; + read.size = size; + read.offset = offset; + + return ioctl(fd, MMFS_IOCTL_PREAD, &read); +} + +int do_write(int fd, int handle, void *buf, int offset, int size) +{ + struct mmfs_pwrite_args write; + + memset(&write, 0, sizeof(write)); + write.handle = handle; + write.data = buf; + write.size = size; + write.offset = offset; + + return ioctl(fd, MMFS_IOCTL_PWRITE, &write); +} + +int main(int argc, char **argv) +{ + int fd; + struct mmfs_alloc_args alloc; + uint8_t expected[MMFS_BUFFER_SIZE]; + uint8_t buf[MMFS_BUFFER_SIZE]; + int ret; + int handle; + + fd = open_mmfs_device(); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = MMFS_BUFFER_SIZE; + ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc); + assert(ret == 0); + handle = alloc.handle; + + printf("Testing contents of newly allocated object.\n"); + ret = do_read(fd, handle, buf, 0, MMFS_BUFFER_SIZE); + assert(ret == 0); + memset(&expected, 0, sizeof(expected)); + assert(memcmp(expected, buf, sizeof(expected)) == 0); + + printf("Testing read beyond end of buffer.\n"); + ret = do_read(fd, handle, buf, MMFS_BUFFER_SIZE / 2, MMFS_BUFFER_SIZE); + assert(ret == -1 && errno == EINVAL); + + printf("Testing full write of buffer\n"); + memset(buf, 0, sizeof(buf)); + memset(buf + 1024, 0x01, 1024); + memset(expected + 1024, 0x01, 1024); + ret = do_write(fd, handle, buf, 0, MMFS_BUFFER_SIZE); + assert(ret == 0); + ret = do_read(fd, handle, buf, 0, MMFS_BUFFER_SIZE); + assert(ret == 0); + assert(memcmp(buf, expected, sizeof(buf)) == 0); + + printf("Testing partial write of buffer\n"); + memset(buf + 4096, 0x02, 1024); + memset(expected + 4096, 0x02, 1024); + ret = do_write(fd, handle, buf + 4096, 4096, 1024); + assert(ret == 0); + ret = do_read(fd, handle, buf, 0, MMFS_BUFFER_SIZE); + assert(ret == 0); + assert(memcmp(buf, expected, sizeof(buf)) == 0); + + printf("Testing partial read of buffer\n"); + ret = do_read(fd, handle, buf, 512, 1024); + assert(ret == 0); + assert(memcmp(buf, expected + 512, 1024) == 0); + + close(fd); + + return 0; +} -- 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. --- .gitignore | 1 + linux-core/mmfs_drv.c | 43 +++++++++++++++++ tests/Makefile.am | 3 +- tests/mmfs_mmap.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 tests/mmfs_mmap.c diff --git a/.gitignore b/.gitignore index 5a4bcb13..4d89a3cb 100644 --- a/.gitignore +++ b/.gitignore @@ -59,6 +59,7 @@ tests/getstats tests/getversion tests/lock tests/mmfs_basic +tests/mmfs_mmap tests/mmfs_readwrite tests/openclose tests/setversion 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; } diff --git a/tests/Makefile.am b/tests/Makefile.am index e2931013..67cb034d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -24,7 +24,8 @@ TESTS = auth \ setversion \ updatedraw \ mmfs_basic \ - mmfs_readwrite + mmfs_readwrite \ + mmfs_mmap EXTRA_PROGRAMS = $(TESTS) CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LTLIBRARIES) diff --git a/tests/mmfs_mmap.c b/tests/mmfs_mmap.c new file mode 100644 index 00000000..3cb6a853 --- /dev/null +++ b/tests/mmfs_mmap.c @@ -0,0 +1,129 @@ +/* + * 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 "mmfs.h" + +#define MMFS_BUFFER_SIZE 16384 + +int do_read(int fd, int handle, void *buf, int offset, int size) +{ + struct mmfs_pread_args read; + + /* Ensure that we don't have any convenient data in buf in case + * we fail. + */ + memset(buf, 0xd0, size); + + memset(&read, 0, sizeof(read)); + read.handle = handle; + read.data = buf; + read.size = size; + read.offset = offset; + + return ioctl(fd, MMFS_IOCTL_PREAD, &read); +} + +int do_write(int fd, int handle, void *buf, int offset, int size) +{ + struct mmfs_pwrite_args write; + + memset(&write, 0, sizeof(write)); + write.handle = handle; + write.data = buf; + write.size = size; + write.offset = offset; + + return ioctl(fd, MMFS_IOCTL_PWRITE, &write); +} + +int main(int argc, char **argv) +{ + int fd; + struct mmfs_alloc_args alloc; + struct mmfs_mmap_args mmap; + struct mmfs_unreference_args unref; + uint8_t expected[MMFS_BUFFER_SIZE]; + uint8_t buf[MMFS_BUFFER_SIZE]; + int ret; + int handle; + + fd = open_mmfs_device(); + + memset(&mmap, 0, sizeof(mmap)); + mmap.handle = 0x10101010; + mmap.offset = 0; + mmap.size = 4096; + printf("Testing mmaping of bad object.\n"); + ret = ioctl(fd, MMFS_IOCTL_MMAP, &mmap); + assert(ret == -1 && errno == EINVAL); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = MMFS_BUFFER_SIZE; + ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc); + assert(ret == 0); + handle = alloc.handle; + + printf("Testing mmaping of newly allocated object.\n"); + mmap.handle = handle; + mmap.offset = 0; + mmap.size = MMFS_BUFFER_SIZE; + ret = ioctl(fd, MMFS_IOCTL_MMAP, &mmap); + assert(ret == 0); + + printf("Testing contents of newly allocated object.\n"); + memset(expected, 0, sizeof(expected)); + assert(memcmp(mmap.addr, expected, sizeof(expected)) == 0); + + printf("Testing coherency of writes and mmap reads.\n"); + memset(buf, 0, sizeof(buf)); + memset(buf + 1024, 0x01, 1024); + memset(expected + 1024, 0x01, 1024); + ret = do_write(fd, handle, buf, 0, MMFS_BUFFER_SIZE); + assert(ret == 0); + assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); + + printf("Testing that mapping stays after unreference\n"); + unref.handle = handle; + ret = ioctl(fd, MMFS_IOCTL_UNREFERENCE, &unref); + assert(ret == 0); + assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); + + printf("Testing unmapping\n"); + munmap(mmap.addr, MMFS_BUFFER_SIZE); + + close(fd); + + return 0; +} -- 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 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 +++++++++++++++++++++++++++++++++++++++++++++ shared-core/drm.h | 68 +++++++++ 6 files changed, 482 insertions(+), 2 deletions(-) create mode 100644 linux-core/drm_mm.c 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); + } +} diff --git a/shared-core/drm.h b/shared-core/drm.h index 5981dcb8..caa3dbd1 100644 --- a/shared-core/drm.h +++ b/shared-core/drm.h @@ -960,6 +960,69 @@ struct drm_mm_info_arg { uint64_t p_size; }; + +struct drm_mm_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 drm_mm_unreference_args { + /** Handle of the object to be unreferenced. */ + uint32_t handle; +}; + +struct drm_mm_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 drm_mm_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 drm_mm_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 drm_mm_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 */ @@ -980,6 +1043,11 @@ struct drm_mm_info_arg { #define DRM_IOCTL_GET_STATS DRM_IOR( 0x06, struct drm_stats) #define DRM_IOCTL_SET_VERSION DRM_IOWR(0x07, struct drm_set_version) #define DRM_IOCTL_MODESET_CTL DRM_IOW(0x08, struct drm_modeset_ctl) +#define DRM_IOCTL_MM_ALLOC DRM_IOWR(0x09, struct drm_mm_alloc_args) +#define DRM_IOCTL_MM_UNREFERENCE DRM_IOW(0x0a, struct drm_mm_unreference_args) +#define DRM_IOCTL_MM_PREAD DRM_IOW(0x0b, struct drm_mm_pread_args) +#define DRM_IOCTL_MM_PWRITE DRM_IOW(0x0c, struct drm_mm_pwrite_args) +#define DRM_IOCTL_MM_MMAP DRM_IOWR(0x0d, struct drm_mm_mmap_args) #define DRM_IOCTL_SET_UNIQUE DRM_IOW( 0x10, struct drm_unique) #define DRM_IOCTL_AUTH_MAGIC DRM_IOW( 0x11, struct drm_auth) -- cgit v1.2.3 From 3148c1636408cc422ab83c149a8963916dd376b0 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 29 Apr 2008 13:45:43 -0700 Subject: Move mmfs tests over to be drm tests. --- .gitignore | 6 +-- tests/Makefile.am | 6 +-- tests/mm_basic.c | 99 +++++++++++++++++++++++++++++++++++++ tests/mm_mmap.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/mm_readwrite.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++ tests/mmfs_basic.c | 112 ------------------------------------------ tests/mmfs_mmap.c | 129 ------------------------------------------------- tests/mmfs_readwrite.c | 125 ----------------------------------------------- 8 files changed, 359 insertions(+), 372 deletions(-) create mode 100644 tests/mm_basic.c create mode 100644 tests/mm_mmap.c create mode 100644 tests/mm_readwrite.c delete mode 100644 tests/mmfs_basic.c delete mode 100644 tests/mmfs_mmap.c delete mode 100644 tests/mmfs_readwrite.c diff --git a/.gitignore b/.gitignore index 4d89a3cb..89172e6c 100644 --- a/.gitignore +++ b/.gitignore @@ -58,9 +58,9 @@ tests/getclient tests/getstats tests/getversion tests/lock -tests/mmfs_basic -tests/mmfs_mmap -tests/mmfs_readwrite +tests/mm_basic +tests/mm_mmap +tests/mm_readwrite tests/openclose tests/setversion tests/updatedraw diff --git a/tests/Makefile.am b/tests/Makefile.am index 67cb034d..f997e26e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -23,9 +23,9 @@ TESTS = auth \ lock \ setversion \ updatedraw \ - mmfs_basic \ - mmfs_readwrite \ - mmfs_mmap + mm_basic \ + mm_readwrite \ + mm_mmap EXTRA_PROGRAMS = $(TESTS) CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LTLIBRARIES) diff --git a/tests/mm_basic.c b/tests/mm_basic.c new file mode 100644 index 00000000..94240ee1 --- /dev/null +++ b/tests/mm_basic.c @@ -0,0 +1,99 @@ +/* + * 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 "drm.h" + +static void +test_bad_unref(int fd) +{ + struct drm_mm_unreference_args unref; + int ret; + + printf("Testing error return on bad unreference ioctl.\n"); + + unref.handle = 0x10101010; + ret = ioctl(fd, DRM_IOCTL_MM_UNREFERENCE, &unref); + + assert(ret == -1 && errno == EINVAL); +} + +static void +test_alloc_unref(int fd) +{ + struct drm_mm_alloc_args alloc; + struct drm_mm_unreference_args unref; + int ret; + + printf("Testing allocating and unreferencing an object.\n"); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = 16 * 1024; + ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc); + assert(ret == 0); + + unref.handle = alloc.handle; + ret = ioctl(fd, DRM_IOCTL_MM_UNREFERENCE, &unref); +} + +static void +test_alloc_close(int fd) +{ + struct drm_mm_alloc_args alloc; + int ret; + + printf("Testing closing with an object allocated.\n"); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = 16 * 1024; + ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc); + assert(ret == 0); + + close(fd); +} + +int main(int argc, char **argv) +{ + int fd; + + fd = drm_open_any(); + + test_bad_unref(fd); + test_alloc_unref(fd); + test_alloc_close(fd); + + close(fd); + + return 0; +} diff --git a/tests/mm_mmap.c b/tests/mm_mmap.c new file mode 100644 index 00000000..e5c4538a --- /dev/null +++ b/tests/mm_mmap.c @@ -0,0 +1,129 @@ +/* + * 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 "drm.h" + +#define OBJECT_SIZE 16384 + +int do_read(int fd, int handle, void *buf, int offset, int size) +{ + struct drm_mm_pread_args read; + + /* Ensure that we don't have any convenient data in buf in case + * we fail. + */ + memset(buf, 0xd0, size); + + memset(&read, 0, sizeof(read)); + read.handle = handle; + read.data = buf; + read.size = size; + read.offset = offset; + + return ioctl(fd, DRM_IOCTL_MM_PREAD, &read); +} + +int do_write(int fd, int handle, void *buf, int offset, int size) +{ + struct drm_mm_pwrite_args write; + + memset(&write, 0, sizeof(write)); + write.handle = handle; + write.data = buf; + write.size = size; + write.offset = offset; + + return ioctl(fd, DRM_IOCTL_MM_PWRITE, &write); +} + +int main(int argc, char **argv) +{ + int fd; + struct drm_mm_alloc_args alloc; + struct drm_mm_mmap_args mmap; + struct drm_mm_unreference_args unref; + uint8_t expected[OBJECT_SIZE]; + uint8_t buf[OBJECT_SIZE]; + int ret; + int handle; + + fd = drm_open_any(); + + memset(&mmap, 0, sizeof(mmap)); + mmap.handle = 0x10101010; + mmap.offset = 0; + mmap.size = 4096; + printf("Testing mmaping of bad object.\n"); + ret = ioctl(fd, DRM_IOCTL_MM_MMAP, &mmap); + assert(ret == -1 && errno == EINVAL); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = OBJECT_SIZE; + ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc); + assert(ret == 0); + handle = alloc.handle; + + printf("Testing mmaping of newly allocated object.\n"); + mmap.handle = handle; + mmap.offset = 0; + mmap.size = OBJECT_SIZE; + ret = ioctl(fd, DRM_IOCTL_MM_MMAP, &mmap); + assert(ret == 0); + + printf("Testing contents of newly allocated object.\n"); + memset(expected, 0, sizeof(expected)); + assert(memcmp(mmap.addr, expected, sizeof(expected)) == 0); + + printf("Testing coherency of writes and mmap reads.\n"); + memset(buf, 0, sizeof(buf)); + memset(buf + 1024, 0x01, 1024); + memset(expected + 1024, 0x01, 1024); + ret = do_write(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); + + printf("Testing that mapping stays after unreference\n"); + unref.handle = handle; + ret = ioctl(fd, DRM_IOCTL_MM_UNREFERENCE, &unref); + assert(ret == 0); + assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); + + printf("Testing unmapping\n"); + munmap(mmap.addr, OBJECT_SIZE); + + close(fd); + + return 0; +} diff --git a/tests/mm_readwrite.c b/tests/mm_readwrite.c new file mode 100644 index 00000000..a778231a --- /dev/null +++ b/tests/mm_readwrite.c @@ -0,0 +1,125 @@ +/* + * 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 "drm.h" + +#define OBJECT_SIZE 16384 + +int do_read(int fd, int handle, void *buf, int offset, int size) +{ + struct drm_mm_pread_args read; + + /* Ensure that we don't have any convenient data in buf in case + * we fail. + */ + memset(buf, 0xd0, size); + + memset(&read, 0, sizeof(read)); + read.handle = handle; + read.data = buf; + read.size = size; + read.offset = offset; + + return ioctl(fd, DRM_IOCTL_MM_PREAD, &read); +} + +int do_write(int fd, int handle, void *buf, int offset, int size) +{ + struct drm_mm_pwrite_args write; + + memset(&write, 0, sizeof(write)); + write.handle = handle; + write.data = buf; + write.size = size; + write.offset = offset; + + return ioctl(fd, DRM_IOCTL_MM_PWRITE, &write); +} + +int main(int argc, char **argv) +{ + int fd; + struct drm_mm_alloc_args alloc; + uint8_t expected[OBJECT_SIZE]; + uint8_t buf[OBJECT_SIZE]; + int ret; + int handle; + + fd = drm_open_any(); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = OBJECT_SIZE; + ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc); + assert(ret == 0); + handle = alloc.handle; + + printf("Testing contents of newly allocated object.\n"); + ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + memset(&expected, 0, sizeof(expected)); + assert(memcmp(expected, buf, sizeof(expected)) == 0); + + printf("Testing read beyond end of buffer.\n"); + ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE); + assert(ret == -1 && errno == EINVAL); + + printf("Testing full write of buffer\n"); + memset(buf, 0, sizeof(buf)); + memset(buf + 1024, 0x01, 1024); + memset(expected + 1024, 0x01, 1024); + ret = do_write(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + assert(memcmp(buf, expected, sizeof(buf)) == 0); + + printf("Testing partial write of buffer\n"); + memset(buf + 4096, 0x02, 1024); + memset(expected + 4096, 0x02, 1024); + ret = do_write(fd, handle, buf + 4096, 4096, 1024); + assert(ret == 0); + ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + assert(memcmp(buf, expected, sizeof(buf)) == 0); + + printf("Testing partial read of buffer\n"); + ret = do_read(fd, handle, buf, 512, 1024); + assert(ret == 0); + assert(memcmp(buf, expected + 512, 1024) == 0); + + close(fd); + + return 0; +} diff --git a/tests/mmfs_basic.c b/tests/mmfs_basic.c deleted file mode 100644 index c6975b0b..00000000 --- a/tests/mmfs_basic.c +++ /dev/null @@ -1,112 +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 "mmfs.h" - -static void -test_bad_unref(int fd) -{ - struct mmfs_unreference_args unref; - int ret; - - printf("Testing error return on bad unreference ioctl.\n"); - - unref.handle = 0x10101010; - ret = ioctl(fd, MMFS_IOCTL_UNREFERENCE, &unref); - - assert(ret == -1 && errno == EINVAL); -} - -static void -test_bad_ioctl(int fd) -{ - int ret; - - printf("Testing error return on bad ioctl.\n"); - - ret = ioctl(fd, _IO(MMFS_IOCTL_BASE, 0xf0), 0); - - assert(ret == -1 && errno == EINVAL); -} - -static void -test_alloc_unref(int fd) -{ - struct mmfs_alloc_args alloc; - struct mmfs_unreference_args unref; - int ret; - - printf("Testing allocating and unreferencing an object.\n"); - - memset(&alloc, 0, sizeof(alloc)); - alloc.size = 16 * 1024; - ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc); - assert(ret == 0); - - unref.handle = alloc.handle; - ret = ioctl(fd, MMFS_IOCTL_UNREFERENCE, &unref); -} - -static void -test_alloc_close(int fd) -{ - struct mmfs_alloc_args alloc; - int ret; - - printf("Testing closing with an object allocated.\n"); - - memset(&alloc, 0, sizeof(alloc)); - alloc.size = 16 * 1024; - ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc); - assert(ret == 0); - - close(fd); -} - -int main(int argc, char **argv) -{ - int fd; - - fd = open_mmfs_device(); - - test_bad_ioctl(fd); - test_bad_unref(fd); - test_alloc_unref(fd); - test_alloc_close(fd); - - close(fd); - - return 0; -} diff --git a/tests/mmfs_mmap.c b/tests/mmfs_mmap.c deleted file mode 100644 index 3cb6a853..00000000 --- a/tests/mmfs_mmap.c +++ /dev/null @@ -1,129 +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 "mmfs.h" - -#define MMFS_BUFFER_SIZE 16384 - -int do_read(int fd, int handle, void *buf, int offset, int size) -{ - struct mmfs_pread_args read; - - /* Ensure that we don't have any convenient data in buf in case - * we fail. - */ - memset(buf, 0xd0, size); - - memset(&read, 0, sizeof(read)); - read.handle = handle; - read.data = buf; - read.size = size; - read.offset = offset; - - return ioctl(fd, MMFS_IOCTL_PREAD, &read); -} - -int do_write(int fd, int handle, void *buf, int offset, int size) -{ - struct mmfs_pwrite_args write; - - memset(&write, 0, sizeof(write)); - write.handle = handle; - write.data = buf; - write.size = size; - write.offset = offset; - - return ioctl(fd, MMFS_IOCTL_PWRITE, &write); -} - -int main(int argc, char **argv) -{ - int fd; - struct mmfs_alloc_args alloc; - struct mmfs_mmap_args mmap; - struct mmfs_unreference_args unref; - uint8_t expected[MMFS_BUFFER_SIZE]; - uint8_t buf[MMFS_BUFFER_SIZE]; - int ret; - int handle; - - fd = open_mmfs_device(); - - memset(&mmap, 0, sizeof(mmap)); - mmap.handle = 0x10101010; - mmap.offset = 0; - mmap.size = 4096; - printf("Testing mmaping of bad object.\n"); - ret = ioctl(fd, MMFS_IOCTL_MMAP, &mmap); - assert(ret == -1 && errno == EINVAL); - - memset(&alloc, 0, sizeof(alloc)); - alloc.size = MMFS_BUFFER_SIZE; - ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc); - assert(ret == 0); - handle = alloc.handle; - - printf("Testing mmaping of newly allocated object.\n"); - mmap.handle = handle; - mmap.offset = 0; - mmap.size = MMFS_BUFFER_SIZE; - ret = ioctl(fd, MMFS_IOCTL_MMAP, &mmap); - assert(ret == 0); - - printf("Testing contents of newly allocated object.\n"); - memset(expected, 0, sizeof(expected)); - assert(memcmp(mmap.addr, expected, sizeof(expected)) == 0); - - printf("Testing coherency of writes and mmap reads.\n"); - memset(buf, 0, sizeof(buf)); - memset(buf + 1024, 0x01, 1024); - memset(expected + 1024, 0x01, 1024); - ret = do_write(fd, handle, buf, 0, MMFS_BUFFER_SIZE); - assert(ret == 0); - assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); - - printf("Testing that mapping stays after unreference\n"); - unref.handle = handle; - ret = ioctl(fd, MMFS_IOCTL_UNREFERENCE, &unref); - assert(ret == 0); - assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); - - printf("Testing unmapping\n"); - munmap(mmap.addr, MMFS_BUFFER_SIZE); - - close(fd); - - return 0; -} diff --git a/tests/mmfs_readwrite.c b/tests/mmfs_readwrite.c deleted file mode 100644 index 09d1967d..00000000 --- a/tests/mmfs_readwrite.c +++ /dev/null @@ -1,125 +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 "mmfs.h" - -#define MMFS_BUFFER_SIZE 16384 - -int do_read(int fd, int handle, void *buf, int offset, int size) -{ - struct mmfs_pread_args read; - - /* Ensure that we don't have any convenient data in buf in case - * we fail. - */ - memset(buf, 0xd0, size); - - memset(&read, 0, sizeof(read)); - read.handle = handle; - read.data = buf; - read.size = size; - read.offset = offset; - - return ioctl(fd, MMFS_IOCTL_PREAD, &read); -} - -int do_write(int fd, int handle, void *buf, int offset, int size) -{ - struct mmfs_pwrite_args write; - - memset(&write, 0, sizeof(write)); - write.handle = handle; - write.data = buf; - write.size = size; - write.offset = offset; - - return ioctl(fd, MMFS_IOCTL_PWRITE, &write); -} - -int main(int argc, char **argv) -{ - int fd; - struct mmfs_alloc_args alloc; - uint8_t expected[MMFS_BUFFER_SIZE]; - uint8_t buf[MMFS_BUFFER_SIZE]; - int ret; - int handle; - - fd = open_mmfs_device(); - - memset(&alloc, 0, sizeof(alloc)); - alloc.size = MMFS_BUFFER_SIZE; - ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc); - assert(ret == 0); - handle = alloc.handle; - - printf("Testing contents of newly allocated object.\n"); - ret = do_read(fd, handle, buf, 0, MMFS_BUFFER_SIZE); - assert(ret == 0); - memset(&expected, 0, sizeof(expected)); - assert(memcmp(expected, buf, sizeof(expected)) == 0); - - printf("Testing read beyond end of buffer.\n"); - ret = do_read(fd, handle, buf, MMFS_BUFFER_SIZE / 2, MMFS_BUFFER_SIZE); - assert(ret == -1 && errno == EINVAL); - - printf("Testing full write of buffer\n"); - memset(buf, 0, sizeof(buf)); - memset(buf + 1024, 0x01, 1024); - memset(expected + 1024, 0x01, 1024); - ret = do_write(fd, handle, buf, 0, MMFS_BUFFER_SIZE); - assert(ret == 0); - ret = do_read(fd, handle, buf, 0, MMFS_BUFFER_SIZE); - assert(ret == 0); - assert(memcmp(buf, expected, sizeof(buf)) == 0); - - printf("Testing partial write of buffer\n"); - memset(buf + 4096, 0x02, 1024); - memset(expected + 4096, 0x02, 1024); - ret = do_write(fd, handle, buf + 4096, 4096, 1024); - assert(ret == 0); - ret = do_read(fd, handle, buf, 0, MMFS_BUFFER_SIZE); - assert(ret == 0); - assert(memcmp(buf, expected, sizeof(buf)) == 0); - - printf("Testing partial read of buffer\n"); - ret = do_read(fd, handle, buf, 512, 1024); - assert(ret == 0); - assert(memcmp(buf, expected + 512, 1024) == 0); - - close(fd); - - return 0; -} -- 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 ------- shared-core/mmfs.h | 146 --------------- tests/drmtest.c | 33 ---- tests/drmtest.h | 1 - 8 files changed, 2 insertions(+), 694 deletions(-) delete mode 120000 linux-core/mmfs.h delete mode 100644 linux-core/mmfs_drv.c delete mode 100644 linux-core/mmfs_drv.h delete mode 100644 shared-core/mmfs.h 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); diff --git a/shared-core/mmfs.h b/shared-core/mmfs.h deleted file mode 100644 index e0a4f25b..00000000 --- a/shared-core/mmfs.h +++ /dev/null @@ -1,146 +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 - * - */ - -#ifdef __linux__ -#include -#else -#include -#include -#endif - -/** @file mmfs.h - * This file provides ioctl and ioctl argument definitions for using the - * mmfs device. - */ - -#ifdef __KERNEL__ -#include "mmfs_drv.h" -#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/tests/drmtest.c b/tests/drmtest.c index 58f71a6a..5453b105 100644 --- a/tests/drmtest.c +++ b/tests/drmtest.c @@ -28,7 +28,6 @@ #include #include #include "drmtest.h" -#include "mmfs.h" /** Open the first DRM device we can find, searching up to 16 device nodes */ int drm_open_any(void) @@ -82,35 +81,3 @@ int drm_open_any_master(void) fprintf(stderr, "Couldn't find an un-controlled DRM device\n"); abort(); } - -static void -create_mmfs_device() -{ - struct stat sb; - int ret; - - ret = stat(MMFS_DEVICE_PATH, &sb); - - if (ret == 0) - return; - - ret = mknod(MMFS_DEVICE_PATH, S_IFCHR | S_IRUSR | S_IWUSR, - makedev(MMFS_DEVICE_MAJOR, 0)); - - if (ret != 0) - errx(1, "mknod()"); -} - -int -open_mmfs_device() -{ - int fd; - - create_mmfs_device(); - - fd = open(MMFS_DEVICE_PATH, O_RDWR); - if (fd == -1) - errx(1, "open()"); - - return fd; -} diff --git a/tests/drmtest.h b/tests/drmtest.h index b84ada71..afa0df4a 100644 --- a/tests/drmtest.h +++ b/tests/drmtest.h @@ -35,4 +35,3 @@ int drm_open_any(void); int drm_open_any_master(void); -int open_mmfs_device(); -- 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 ++++++++++++++ shared-core/i915_dma.c | 2 ++ shared-core/i915_drm.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++ shared-core/i915_drv.h | 9 +++++ 6 files changed, 126 insertions(+), 1 deletion(-) 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) diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 7ccd185c..6c6fd435 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1124,6 +1124,8 @@ struct drm_ioctl_desc i915_ioctls[] = { #ifdef I915_HAVE_BUFFER DRM_IOCTL_DEF(DRM_I915_EXECBUFFER, i915_execbuffer, DRM_AUTH), #endif + DRM_IOCTL_DEF(DRM_I915_MM_INIT, intel_mm_init_ioctl, DRM_AUTH), + DRM_IOCTL_DEF(DRM_I915_MM_EXECBUFFER, intel_mm_execbuffer, DRM_AUTH), }; int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls); diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index de463614..1a70010b 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -176,6 +176,8 @@ typedef struct drm_i915_sarea { #define DRM_I915_MMIO 0x10 #define DRM_I915_HWS_ADDR 0x11 #define DRM_I915_EXECBUFFER 0x12 +#define DRM_I915_MM_INIT 0x13 +#define DRM_I915_MM_EXECBUFFER 0x14 #define DRM_IOCTL_I915_INIT DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t) #define DRM_IOCTL_I915_FLUSH DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH) @@ -195,6 +197,8 @@ typedef struct drm_i915_sarea { #define DRM_IOCTL_I915_VBLANK_SWAP DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_VBLANK_SWAP, drm_i915_vblank_swap_t) #define DRM_IOCTL_I915_MMIO DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_MMIO, drm_i915_mmio) #define DRM_IOCTL_I915_EXECBUFFER DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_EXECBUFFER, struct drm_i915_execbuffer) +#define DRM_IOCTL_I915_MM_INIT DRM_IOW(DRM_COMMAND_BASE + DRM_I915_MM_INIT, struct drm_i915_mm_init) +#define DRM_IOCTL_I915_MM_EXECBUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_I915_MM_INIT, struct drm_i915_mm_execbuffer) /* Asynchronous page flipping: */ @@ -394,4 +398,85 @@ struct drm_i915_execbuffer { struct drm_fence_arg fence_arg; }; +struct drm_i915_mm_init { + /** + * Beginning offset in the GTT to be managed by the DRM memory + * manager. + */ + off_t gtt_start; + /** + * Ending offset in the GTT to be managed by the DRM memory + * manager. + */ + off_t gtt_end; +}; + +struct drm_i915_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_buffer; + + /** Offset in the buffer the relocation entry will be written into */ + uint32_t offset; + + /** + * Value to be added to the offset of the target buffer to make up + * the relocation entry. + */ + uint32_t delta; + + /** + * 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. + */ + uint32_t presumed_offset; +}; + +struct drm_i915_mm_validate_entry { + /** + * User's handle for a buffer to be bound into the GTT for this + * operation. + */ + uint32_t buffer_handle; + /** + * Returned value of the updated offset of the buffer, for future + * presumed_offset writes. + */ + uint32_t buffer_offset; + /** List of relocations to be performed on this buffer */ + struct drm_i915_relocation_entry *relocs; + uint32_t relocation_count; +}; + +struct drm_i915_mm_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_mm_validate_entry *buffers; + 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; + struct drm_clip_rect *cliprects; +}; + #endif /* _I915_DRM_H_ */ diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 412a2594..0c6ec588 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -241,6 +241,10 @@ typedef struct drm_i915_private { u8 saveDACMASK; u8 saveDACDATA[256*3]; /* 256 3-byte colors */ u8 saveCR[36]; + + struct { + struct drm_memrange gtt_space; + } mm; } drm_i915_private_t; enum intel_chip_family { @@ -329,6 +333,11 @@ void i915_flush_ttm(struct drm_ttm *ttm); /* i915_execbuf.c */ int i915_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv); +/* i915_mm.c */ +int intel_mm_init_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int intel_mm_execbuffer(struct drm_device *dev, void *data, + struct drm_file *file_priv); #endif -- 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 +++++++++++++++++ shared-core/drm.h | 22 +-- shared-core/i915_dma.c | 4 +- shared-core/i915_drm.h | 20 +-- shared-core/i915_drv.h | 20 ++- 12 files changed, 594 insertions(+), 411 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 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); +} + diff --git a/shared-core/drm.h b/shared-core/drm.h index caa3dbd1..d79fa5f8 100644 --- a/shared-core/drm.h +++ b/shared-core/drm.h @@ -961,7 +961,7 @@ struct drm_mm_info_arg { }; -struct drm_mm_alloc_args { +struct drm_gem_alloc_args { /** * Requested size for the object. * @@ -972,12 +972,12 @@ struct drm_mm_alloc_args { uint32_t handle; }; -struct drm_mm_unreference_args { +struct drm_gem_unreference_args { /** Handle of the object to be unreferenced. */ uint32_t handle; }; -struct drm_mm_link_args { +struct drm_gem_link_args { /** Handle for the object being given a name. */ uint32_t handle; /** Requested file name to export the object under. */ @@ -986,7 +986,7 @@ struct drm_mm_link_args { mode_t mode; }; -struct drm_mm_pread_args { +struct drm_gem_pread_args { /** Handle for the object being read. */ uint32_t handle; /** Offset into the object to read from */ @@ -997,7 +997,7 @@ struct drm_mm_pread_args { void *data; }; -struct drm_mm_pwrite_args { +struct drm_gem_pwrite_args { /** Handle for the object being written to. */ uint32_t handle; /** Offset into the object to write to */ @@ -1008,7 +1008,7 @@ struct drm_mm_pwrite_args { void *data; }; -struct drm_mm_mmap_args { +struct drm_gem_mmap_args { /** Handle for the object being mapped. */ uint32_t handle; /** Offset in the object to map. */ @@ -1043,11 +1043,11 @@ struct drm_mm_mmap_args { #define DRM_IOCTL_GET_STATS DRM_IOR( 0x06, struct drm_stats) #define DRM_IOCTL_SET_VERSION DRM_IOWR(0x07, struct drm_set_version) #define DRM_IOCTL_MODESET_CTL DRM_IOW(0x08, struct drm_modeset_ctl) -#define DRM_IOCTL_MM_ALLOC DRM_IOWR(0x09, struct drm_mm_alloc_args) -#define DRM_IOCTL_MM_UNREFERENCE DRM_IOW(0x0a, struct drm_mm_unreference_args) -#define DRM_IOCTL_MM_PREAD DRM_IOW(0x0b, struct drm_mm_pread_args) -#define DRM_IOCTL_MM_PWRITE DRM_IOW(0x0c, struct drm_mm_pwrite_args) -#define DRM_IOCTL_MM_MMAP DRM_IOWR(0x0d, struct drm_mm_mmap_args) +#define DRM_IOCTL_GEM_ALLOC DRM_IOWR(0x09, struct drm_gem_alloc_args) +#define DRM_IOCTL_GEM_UNREFERENCE DRM_IOW(0x0a, struct drm_gem_unreference_args) +#define DRM_IOCTL_GEM_PREAD DRM_IOW(0x0b, struct drm_gem_pread_args) +#define DRM_IOCTL_GEM_PWRITE DRM_IOW(0x0c, struct drm_gem_pwrite_args) +#define DRM_IOCTL_GEM_MMAP DRM_IOWR(0x0d, struct drm_gem_mmap_args) #define DRM_IOCTL_SET_UNIQUE DRM_IOW( 0x10, struct drm_unique) #define DRM_IOCTL_AUTH_MAGIC DRM_IOW( 0x11, struct drm_auth) diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 6c6fd435..821b2a95 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1124,8 +1124,8 @@ struct drm_ioctl_desc i915_ioctls[] = { #ifdef I915_HAVE_BUFFER DRM_IOCTL_DEF(DRM_I915_EXECBUFFER, i915_execbuffer, DRM_AUTH), #endif - DRM_IOCTL_DEF(DRM_I915_MM_INIT, intel_mm_init_ioctl, DRM_AUTH), - DRM_IOCTL_DEF(DRM_I915_MM_EXECBUFFER, intel_mm_execbuffer, DRM_AUTH), + DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH), + DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH), }; int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls); diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 1a70010b..4c241fc5 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -176,8 +176,8 @@ typedef struct drm_i915_sarea { #define DRM_I915_MMIO 0x10 #define DRM_I915_HWS_ADDR 0x11 #define DRM_I915_EXECBUFFER 0x12 -#define DRM_I915_MM_INIT 0x13 -#define DRM_I915_MM_EXECBUFFER 0x14 +#define DRM_I915_GEM_INIT 0x13 +#define DRM_I915_GEM_EXECBUFFER 0x14 #define DRM_IOCTL_I915_INIT DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t) #define DRM_IOCTL_I915_FLUSH DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH) @@ -197,8 +197,8 @@ typedef struct drm_i915_sarea { #define DRM_IOCTL_I915_VBLANK_SWAP DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_VBLANK_SWAP, drm_i915_vblank_swap_t) #define DRM_IOCTL_I915_MMIO DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_MMIO, drm_i915_mmio) #define DRM_IOCTL_I915_EXECBUFFER DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_EXECBUFFER, struct drm_i915_execbuffer) -#define DRM_IOCTL_I915_MM_INIT DRM_IOW(DRM_COMMAND_BASE + DRM_I915_MM_INIT, struct drm_i915_mm_init) -#define DRM_IOCTL_I915_MM_EXECBUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_I915_MM_INIT, struct drm_i915_mm_execbuffer) +#define DRM_IOCTL_I915_MM_INIT DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_INIT, struct drm_i915_gem_init) +#define DRM_IOCTL_I915_MM_EXECBUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_INIT, struct drm_i915_gem_execbuffer) /* Asynchronous page flipping: */ @@ -398,7 +398,7 @@ struct drm_i915_execbuffer { struct drm_fence_arg fence_arg; }; -struct drm_i915_mm_init { +struct drm_i915_gem_init { /** * Beginning offset in the GTT to be managed by the DRM memory * manager. @@ -411,7 +411,7 @@ struct drm_i915_mm_init { off_t gtt_end; }; -struct drm_i915_relocation_entry { +struct drm_i915_gem_relocation_entry { /** * Handle of the buffer being pointed to by this relocation entry. * @@ -441,7 +441,7 @@ struct drm_i915_relocation_entry { uint32_t presumed_offset; }; -struct drm_i915_mm_validate_entry { +struct drm_i915_gem_validate_entry { /** * User's handle for a buffer to be bound into the GTT for this * operation. @@ -453,11 +453,11 @@ struct drm_i915_mm_validate_entry { */ uint32_t buffer_offset; /** List of relocations to be performed on this buffer */ - struct drm_i915_relocation_entry *relocs; + struct drm_i915_gem_relocation_entry *relocs; uint32_t relocation_count; }; -struct drm_i915_mm_execbuffer { +struct drm_i915_gem_execbuffer { /** * List of buffers to be validated wit their relocations to be * performend on them. @@ -466,7 +466,7 @@ struct drm_i915_mm_execbuffer { * a buffer is performing refer to buffers that have already appeared * in the validate list. */ - struct drm_i915_mm_validate_entry *buffers; + struct drm_i915_gem_validate_entry *buffers; uint32_t buffer_count; /** Offset in the batchbuffer to start execution from. */ diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 0c6ec588..7913f48b 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -254,6 +254,18 @@ enum intel_chip_family { CHIP_I965 = 0x08, }; +/** driver private structure attached to each drm_gem_object */ +struct drm_i915_gem_object { + /** Current offset of the object in GTT space, if any. */ + uint32_t gtt_offset; + + /** Boolean whether this object has a valid gtt offset. */ + int gtt_bound; + + /** How many users have pinned this object in GTT space */ + int pin_count; +}; + extern struct drm_ioctl_desc i915_ioctls[]; extern int i915_max_ioctl; @@ -333,11 +345,13 @@ void i915_flush_ttm(struct drm_ttm *ttm); /* i915_execbuf.c */ int i915_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv); -/* i915_mm.c */ -int intel_mm_init_ioctl(struct drm_device *dev, void *data, +/* i915_gem.c */ +int i915_gem_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); -int intel_mm_execbuffer(struct drm_device *dev, void *data, +int i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv); +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); #endif -- 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 ++++++++++++++++++++++++++++++++++++++++---- shared-core/i915_drv.h | 14 +++- 5 files changed, 228 insertions(+), 17 deletions(-) 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) diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 7913f48b..431fc433 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -256,7 +256,19 @@ enum intel_chip_family { /** driver private structure attached to each drm_gem_object */ struct drm_i915_gem_object { - /** Current offset of the object in GTT space, if any. */ + /** Current space allocated to this object in the GTT, if any. */ + struct drm_memrange_node *gtt_space; + + /** AGP memory structure for our GTT binding. */ + DRM_AGP_MEM *agp_mem; + + struct page **page_list; + + /** + * Current offset of the object in GTT space. + * + * This is the same as gtt_space->start + */ uint32_t gtt_offset; /** Boolean whether this object has a valid gtt offset. */ -- 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 ++++++++++++++++++++++++++++++++++++-------- shared-core/i915_drm.h | 2 +- 6 files changed, 98 insertions(+), 25 deletions(-) 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++) diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 4c241fc5..52d1f31f 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -419,7 +419,7 @@ struct drm_i915_gem_relocation_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_buffer; + uint32_t target_handle; /** Offset in the buffer the relocation entry will be written into */ uint32_t offset; -- 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 ++++++++++++++++++++++++++++++++++++++++++- shared-core/i915_dma.c | 2 ++ shared-core/i915_drm.h | 22 ++++++++++++++++++++-- shared-core/i915_drv.h | 4 ++++ 4 files changed, 68 insertions(+), 3 deletions(-) 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); } - diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 821b2a95..73f843c8 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1126,6 +1126,8 @@ struct drm_ioctl_desc i915_ioctls[] = { #endif DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH), DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH), + DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), + DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), }; int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls); diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 52d1f31f..4d113e4c 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -178,6 +178,8 @@ typedef struct drm_i915_sarea { #define DRM_I915_EXECBUFFER 0x12 #define DRM_I915_GEM_INIT 0x13 #define DRM_I915_GEM_EXECBUFFER 0x14 +#define DRM_I915_GEM_PIN 0x15 +#define DRM_I915_GEM_UNPIN 0x16 #define DRM_IOCTL_I915_INIT DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t) #define DRM_IOCTL_I915_FLUSH DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH) @@ -197,8 +199,10 @@ typedef struct drm_i915_sarea { #define DRM_IOCTL_I915_VBLANK_SWAP DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_VBLANK_SWAP, drm_i915_vblank_swap_t) #define DRM_IOCTL_I915_MMIO DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_MMIO, drm_i915_mmio) #define DRM_IOCTL_I915_EXECBUFFER DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_EXECBUFFER, struct drm_i915_execbuffer) -#define DRM_IOCTL_I915_MM_INIT DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_INIT, struct drm_i915_gem_init) -#define DRM_IOCTL_I915_MM_EXECBUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_INIT, struct drm_i915_gem_execbuffer) +#define DRM_IOCTL_I915_GEM_INIT DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_INIT, struct drm_i915_gem_init) +#define DRM_IOCTL_I915_GEM_EXECBUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_EXECBUFFER, struct drm_i915_gem_execbuffer) +#define DRM_IOCTL_I915_GEM_PIN DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_PIN, struct drm_i915_gem_pin) +#define DRM_IOCTL_I915_GEM_UNPIN DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_UNPIN, struct drm_i915_gem_unpin) /* Asynchronous page flipping: */ @@ -479,4 +483,18 @@ struct drm_i915_gem_execbuffer { struct drm_clip_rect *cliprects; }; +struct drm_i915_gem_pin { + /** Handle of the buffer to be pinned. */ + uint32_t handle; + + /** Returned GTT offset of the buffer. */ + uint64_t offset; +}; + +struct drm_i915_gem_unpin { + /** Handle of the buffer to be unpinned. */ + uint32_t handle; +}; + + #endif /* _I915_DRM_H_ */ diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 431fc433..801ac99e 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -362,6 +362,10 @@ int i915_gem_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int i915_gem_execbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv); +int i915_gem_pin_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); +int i915_gem_unpin_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); 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); -- 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 ++++---- shared-core/drm.h | 24 ++++++++++++------------ 3 files changed, 21 insertions(+), 21 deletions(-) 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; diff --git a/shared-core/drm.h b/shared-core/drm.h index d79fa5f8..6d62af6a 100644 --- a/shared-core/drm.h +++ b/shared-core/drm.h @@ -961,7 +961,7 @@ struct drm_mm_info_arg { }; -struct drm_gem_alloc_args { +struct drm_gem_alloc { /** * Requested size for the object. * @@ -972,12 +972,12 @@ struct drm_gem_alloc_args { uint32_t handle; }; -struct drm_gem_unreference_args { +struct drm_gem_unreference { /** Handle of the object to be unreferenced. */ uint32_t handle; }; -struct drm_gem_link_args { +struct drm_gem_link { /** Handle for the object being given a name. */ uint32_t handle; /** Requested file name to export the object under. */ @@ -986,7 +986,7 @@ struct drm_gem_link_args { mode_t mode; }; -struct drm_gem_pread_args { +struct drm_gem_pread { /** Handle for the object being read. */ uint32_t handle; /** Offset into the object to read from */ @@ -997,7 +997,7 @@ struct drm_gem_pread_args { void *data; }; -struct drm_gem_pwrite_args { +struct drm_gem_pwrite { /** Handle for the object being written to. */ uint32_t handle; /** Offset into the object to write to */ @@ -1008,7 +1008,7 @@ struct drm_gem_pwrite_args { void *data; }; -struct drm_gem_mmap_args { +struct drm_gem_mmap { /** Handle for the object being mapped. */ uint32_t handle; /** Offset in the object to map. */ @@ -1042,12 +1042,12 @@ struct drm_gem_mmap_args { #define DRM_IOCTL_GET_CLIENT DRM_IOWR(0x05, struct drm_client) #define DRM_IOCTL_GET_STATS DRM_IOR( 0x06, struct drm_stats) #define DRM_IOCTL_SET_VERSION DRM_IOWR(0x07, struct drm_set_version) -#define DRM_IOCTL_MODESET_CTL DRM_IOW(0x08, struct drm_modeset_ctl) -#define DRM_IOCTL_GEM_ALLOC DRM_IOWR(0x09, struct drm_gem_alloc_args) -#define DRM_IOCTL_GEM_UNREFERENCE DRM_IOW(0x0a, struct drm_gem_unreference_args) -#define DRM_IOCTL_GEM_PREAD DRM_IOW(0x0b, struct drm_gem_pread_args) -#define DRM_IOCTL_GEM_PWRITE DRM_IOW(0x0c, struct drm_gem_pwrite_args) -#define DRM_IOCTL_GEM_MMAP DRM_IOWR(0x0d, struct drm_gem_mmap_args) +#define DRM_IOCTL_MODESET_CTL DRM_IOW(0x08, struct drm_modeset_ctl) +#define DRM_IOCTL_GEM_ALLOC DRM_IOWR(0x09, struct drm_gem_alloc) +#define DRM_IOCTL_GEM_UNREFERENCE DRM_IOW(0x0a, struct drm_gem_unreference) +#define DRM_IOCTL_GEM_PREAD DRM_IOW(0x0b, struct drm_gem_pread) +#define DRM_IOCTL_GEM_PWRITE DRM_IOW(0x0c, struct drm_gem_pwrite) +#define DRM_IOCTL_GEM_MMAP DRM_IOWR(0x0d, struct drm_gem_mmap) #define DRM_IOCTL_SET_UNIQUE DRM_IOW( 0x10, struct drm_unique) #define DRM_IOCTL_AUTH_MAGIC DRM_IOW( 0x11, struct drm_auth) -- cgit v1.2.3 From c530011aaaf485157ba6284c0c32c0db83523b64 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 1 May 2008 16:35:12 -0700 Subject: Update mm tests for GEM rename. --- .gitignore | 6 +-- tests/Makefile.am | 6 +-- tests/gem_basic.c | 99 ++++++++++++++++++++++++++++++++++++++ tests/gem_mmap.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/gem_readwrite.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/mm_basic.c | 99 -------------------------------------- tests/mm_mmap.c | 129 -------------------------------------------------- tests/mm_readwrite.c | 125 ------------------------------------------------ 8 files changed, 359 insertions(+), 359 deletions(-) create mode 100644 tests/gem_basic.c create mode 100644 tests/gem_mmap.c create mode 100644 tests/gem_readwrite.c delete mode 100644 tests/mm_basic.c delete mode 100644 tests/mm_mmap.c delete mode 100644 tests/mm_readwrite.c diff --git a/.gitignore b/.gitignore index 89172e6c..c8a22ea3 100644 --- a/.gitignore +++ b/.gitignore @@ -58,9 +58,9 @@ tests/getclient tests/getstats tests/getversion tests/lock -tests/mm_basic -tests/mm_mmap -tests/mm_readwrite +tests/gem_basic +tests/gem_mmap +tests/gem_readwrite tests/openclose tests/setversion tests/updatedraw diff --git a/tests/Makefile.am b/tests/Makefile.am index f997e26e..718cc436 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -23,9 +23,9 @@ TESTS = auth \ lock \ setversion \ updatedraw \ - mm_basic \ - mm_readwrite \ - mm_mmap + gem_basic \ + gem_readwrite \ + gem_mmap EXTRA_PROGRAMS = $(TESTS) CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LTLIBRARIES) diff --git a/tests/gem_basic.c b/tests/gem_basic.c new file mode 100644 index 00000000..6e1f3ddb --- /dev/null +++ b/tests/gem_basic.c @@ -0,0 +1,99 @@ +/* + * 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 "drm.h" + +static void +test_bad_unref(int fd) +{ + struct drm_gem_unreference unref; + int ret; + + printf("Testing error return on bad unreference ioctl.\n"); + + unref.handle = 0x10101010; + ret = ioctl(fd, DRM_IOCTL_GEM_UNREFERENCE, &unref); + + assert(ret == -1 && errno == EINVAL); +} + +static void +test_alloc_unref(int fd) +{ + struct drm_gem_alloc alloc; + struct drm_gem_unreference unref; + int ret; + + printf("Testing allocating and unreferencing an object.\n"); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = 16 * 1024; + ret = ioctl(fd, DRM_IOCTL_GEM_ALLOC, &alloc); + assert(ret == 0); + + unref.handle = alloc.handle; + ret = ioctl(fd, DRM_IOCTL_GEM_UNREFERENCE, &unref); +} + +static void +test_alloc_close(int fd) +{ + struct drm_gem_alloc alloc; + int ret; + + printf("Testing closing with an object allocated.\n"); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = 16 * 1024; + ret = ioctl(fd, DRM_IOCTL_GEM_ALLOC, &alloc); + assert(ret == 0); + + close(fd); +} + +int main(int argc, char **argv) +{ + int fd; + + fd = drm_open_any(); + + test_bad_unref(fd); + test_alloc_unref(fd); + test_alloc_close(fd); + + close(fd); + + return 0; +} diff --git a/tests/gem_mmap.c b/tests/gem_mmap.c new file mode 100644 index 00000000..2cdc9343 --- /dev/null +++ b/tests/gem_mmap.c @@ -0,0 +1,129 @@ +/* + * 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 "drm.h" + +#define OBJECT_SIZE 16384 + +int do_read(int fd, int handle, void *buf, int offset, int size) +{ + struct drm_gem_pread read; + + /* Ensure that we don't have any convenient data in buf in case + * we fail. + */ + memset(buf, 0xd0, size); + + memset(&read, 0, sizeof(read)); + read.handle = handle; + read.data = buf; + read.size = size; + read.offset = offset; + + return ioctl(fd, DRM_IOCTL_GEM_PREAD, &read); +} + +int do_write(int fd, int handle, void *buf, int offset, int size) +{ + struct drm_gem_pwrite write; + + memset(&write, 0, sizeof(write)); + write.handle = handle; + write.data = buf; + write.size = size; + write.offset = offset; + + return ioctl(fd, DRM_IOCTL_GEM_PWRITE, &write); +} + +int main(int argc, char **argv) +{ + int fd; + struct drm_gem_alloc alloc; + struct drm_gem_mmap mmap; + struct drm_gem_unreference unref; + uint8_t expected[OBJECT_SIZE]; + uint8_t buf[OBJECT_SIZE]; + int ret; + int handle; + + fd = drm_open_any(); + + memset(&mmap, 0, sizeof(mmap)); + mmap.handle = 0x10101010; + mmap.offset = 0; + mmap.size = 4096; + printf("Testing mmaping of bad object.\n"); + ret = ioctl(fd, DRM_IOCTL_GEM_MMAP, &mmap); + assert(ret == -1 && errno == EINVAL); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = OBJECT_SIZE; + ret = ioctl(fd, DRM_IOCTL_GEM_ALLOC, &alloc); + assert(ret == 0); + handle = alloc.handle; + + printf("Testing mmaping of newly allocated object.\n"); + mmap.handle = handle; + mmap.offset = 0; + mmap.size = OBJECT_SIZE; + ret = ioctl(fd, DRM_IOCTL_GEM_MMAP, &mmap); + assert(ret == 0); + + printf("Testing contents of newly allocated object.\n"); + memset(expected, 0, sizeof(expected)); + assert(memcmp(mmap.addr, expected, sizeof(expected)) == 0); + + printf("Testing coherency of writes and mmap reads.\n"); + memset(buf, 0, sizeof(buf)); + memset(buf + 1024, 0x01, 1024); + memset(expected + 1024, 0x01, 1024); + ret = do_write(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); + + printf("Testing that mapping stays after unreference\n"); + unref.handle = handle; + ret = ioctl(fd, DRM_IOCTL_GEM_UNREFERENCE, &unref); + assert(ret == 0); + assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); + + printf("Testing unmapping\n"); + munmap(mmap.addr, OBJECT_SIZE); + + close(fd); + + return 0; +} diff --git a/tests/gem_readwrite.c b/tests/gem_readwrite.c new file mode 100644 index 00000000..5065732d --- /dev/null +++ b/tests/gem_readwrite.c @@ -0,0 +1,125 @@ +/* + * 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 "drm.h" + +#define OBJECT_SIZE 16384 + +int do_read(int fd, int handle, void *buf, int offset, int size) +{ + struct drm_gem_pread read; + + /* Ensure that we don't have any convenient data in buf in case + * we fail. + */ + memset(buf, 0xd0, size); + + memset(&read, 0, sizeof(read)); + read.handle = handle; + read.data = buf; + read.size = size; + read.offset = offset; + + return ioctl(fd, DRM_IOCTL_GEM_PREAD, &read); +} + +int do_write(int fd, int handle, void *buf, int offset, int size) +{ + struct drm_gem_pwrite write; + + memset(&write, 0, sizeof(write)); + write.handle = handle; + write.data = buf; + write.size = size; + write.offset = offset; + + return ioctl(fd, DRM_IOCTL_GEM_PWRITE, &write); +} + +int main(int argc, char **argv) +{ + int fd; + struct drm_gem_alloc alloc; + uint8_t expected[OBJECT_SIZE]; + uint8_t buf[OBJECT_SIZE]; + int ret; + int handle; + + fd = drm_open_any(); + + memset(&alloc, 0, sizeof(alloc)); + alloc.size = OBJECT_SIZE; + ret = ioctl(fd, DRM_IOCTL_GEM_ALLOC, &alloc); + assert(ret == 0); + handle = alloc.handle; + + printf("Testing contents of newly allocated object.\n"); + ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + memset(&expected, 0, sizeof(expected)); + assert(memcmp(expected, buf, sizeof(expected)) == 0); + + printf("Testing read beyond end of buffer.\n"); + ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE); + assert(ret == -1 && errno == EINVAL); + + printf("Testing full write of buffer\n"); + memset(buf, 0, sizeof(buf)); + memset(buf + 1024, 0x01, 1024); + memset(expected + 1024, 0x01, 1024); + ret = do_write(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + assert(memcmp(buf, expected, sizeof(buf)) == 0); + + printf("Testing partial write of buffer\n"); + memset(buf + 4096, 0x02, 1024); + memset(expected + 4096, 0x02, 1024); + ret = do_write(fd, handle, buf + 4096, 4096, 1024); + assert(ret == 0); + ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); + assert(ret == 0); + assert(memcmp(buf, expected, sizeof(buf)) == 0); + + printf("Testing partial read of buffer\n"); + ret = do_read(fd, handle, buf, 512, 1024); + assert(ret == 0); + assert(memcmp(buf, expected + 512, 1024) == 0); + + close(fd); + + return 0; +} diff --git a/tests/mm_basic.c b/tests/mm_basic.c deleted file mode 100644 index 94240ee1..00000000 --- a/tests/mm_basic.c +++ /dev/null @@ -1,99 +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 "drm.h" - -static void -test_bad_unref(int fd) -{ - struct drm_mm_unreference_args unref; - int ret; - - printf("Testing error return on bad unreference ioctl.\n"); - - unref.handle = 0x10101010; - ret = ioctl(fd, DRM_IOCTL_MM_UNREFERENCE, &unref); - - assert(ret == -1 && errno == EINVAL); -} - -static void -test_alloc_unref(int fd) -{ - struct drm_mm_alloc_args alloc; - struct drm_mm_unreference_args unref; - int ret; - - printf("Testing allocating and unreferencing an object.\n"); - - memset(&alloc, 0, sizeof(alloc)); - alloc.size = 16 * 1024; - ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc); - assert(ret == 0); - - unref.handle = alloc.handle; - ret = ioctl(fd, DRM_IOCTL_MM_UNREFERENCE, &unref); -} - -static void -test_alloc_close(int fd) -{ - struct drm_mm_alloc_args alloc; - int ret; - - printf("Testing closing with an object allocated.\n"); - - memset(&alloc, 0, sizeof(alloc)); - alloc.size = 16 * 1024; - ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc); - assert(ret == 0); - - close(fd); -} - -int main(int argc, char **argv) -{ - int fd; - - fd = drm_open_any(); - - test_bad_unref(fd); - test_alloc_unref(fd); - test_alloc_close(fd); - - close(fd); - - return 0; -} diff --git a/tests/mm_mmap.c b/tests/mm_mmap.c deleted file mode 100644 index e5c4538a..00000000 --- a/tests/mm_mmap.c +++ /dev/null @@ -1,129 +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 "drm.h" - -#define OBJECT_SIZE 16384 - -int do_read(int fd, int handle, void *buf, int offset, int size) -{ - struct drm_mm_pread_args read; - - /* Ensure that we don't have any convenient data in buf in case - * we fail. - */ - memset(buf, 0xd0, size); - - memset(&read, 0, sizeof(read)); - read.handle = handle; - read.data = buf; - read.size = size; - read.offset = offset; - - return ioctl(fd, DRM_IOCTL_MM_PREAD, &read); -} - -int do_write(int fd, int handle, void *buf, int offset, int size) -{ - struct drm_mm_pwrite_args write; - - memset(&write, 0, sizeof(write)); - write.handle = handle; - write.data = buf; - write.size = size; - write.offset = offset; - - return ioctl(fd, DRM_IOCTL_MM_PWRITE, &write); -} - -int main(int argc, char **argv) -{ - int fd; - struct drm_mm_alloc_args alloc; - struct drm_mm_mmap_args mmap; - struct drm_mm_unreference_args unref; - uint8_t expected[OBJECT_SIZE]; - uint8_t buf[OBJECT_SIZE]; - int ret; - int handle; - - fd = drm_open_any(); - - memset(&mmap, 0, sizeof(mmap)); - mmap.handle = 0x10101010; - mmap.offset = 0; - mmap.size = 4096; - printf("Testing mmaping of bad object.\n"); - ret = ioctl(fd, DRM_IOCTL_MM_MMAP, &mmap); - assert(ret == -1 && errno == EINVAL); - - memset(&alloc, 0, sizeof(alloc)); - alloc.size = OBJECT_SIZE; - ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc); - assert(ret == 0); - handle = alloc.handle; - - printf("Testing mmaping of newly allocated object.\n"); - mmap.handle = handle; - mmap.offset = 0; - mmap.size = OBJECT_SIZE; - ret = ioctl(fd, DRM_IOCTL_MM_MMAP, &mmap); - assert(ret == 0); - - printf("Testing contents of newly allocated object.\n"); - memset(expected, 0, sizeof(expected)); - assert(memcmp(mmap.addr, expected, sizeof(expected)) == 0); - - printf("Testing coherency of writes and mmap reads.\n"); - memset(buf, 0, sizeof(buf)); - memset(buf + 1024, 0x01, 1024); - memset(expected + 1024, 0x01, 1024); - ret = do_write(fd, handle, buf, 0, OBJECT_SIZE); - assert(ret == 0); - assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); - - printf("Testing that mapping stays after unreference\n"); - unref.handle = handle; - ret = ioctl(fd, DRM_IOCTL_MM_UNREFERENCE, &unref); - assert(ret == 0); - assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); - - printf("Testing unmapping\n"); - munmap(mmap.addr, OBJECT_SIZE); - - close(fd); - - return 0; -} diff --git a/tests/mm_readwrite.c b/tests/mm_readwrite.c deleted file mode 100644 index a778231a..00000000 --- a/tests/mm_readwrite.c +++ /dev/null @@ -1,125 +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 "drm.h" - -#define OBJECT_SIZE 16384 - -int do_read(int fd, int handle, void *buf, int offset, int size) -{ - struct drm_mm_pread_args read; - - /* Ensure that we don't have any convenient data in buf in case - * we fail. - */ - memset(buf, 0xd0, size); - - memset(&read, 0, sizeof(read)); - read.handle = handle; - read.data = buf; - read.size = size; - read.offset = offset; - - return ioctl(fd, DRM_IOCTL_MM_PREAD, &read); -} - -int do_write(int fd, int handle, void *buf, int offset, int size) -{ - struct drm_mm_pwrite_args write; - - memset(&write, 0, sizeof(write)); - write.handle = handle; - write.data = buf; - write.size = size; - write.offset = offset; - - return ioctl(fd, DRM_IOCTL_MM_PWRITE, &write); -} - -int main(int argc, char **argv) -{ - int fd; - struct drm_mm_alloc_args alloc; - uint8_t expected[OBJECT_SIZE]; - uint8_t buf[OBJECT_SIZE]; - int ret; - int handle; - - fd = drm_open_any(); - - memset(&alloc, 0, sizeof(alloc)); - alloc.size = OBJECT_SIZE; - ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc); - assert(ret == 0); - handle = alloc.handle; - - printf("Testing contents of newly allocated object.\n"); - ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); - assert(ret == 0); - memset(&expected, 0, sizeof(expected)); - assert(memcmp(expected, buf, sizeof(expected)) == 0); - - printf("Testing read beyond end of buffer.\n"); - ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE); - assert(ret == -1 && errno == EINVAL); - - printf("Testing full write of buffer\n"); - memset(buf, 0, sizeof(buf)); - memset(buf + 1024, 0x01, 1024); - memset(expected + 1024, 0x01, 1024); - ret = do_write(fd, handle, buf, 0, OBJECT_SIZE); - assert(ret == 0); - ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); - assert(ret == 0); - assert(memcmp(buf, expected, sizeof(buf)) == 0); - - printf("Testing partial write of buffer\n"); - memset(buf + 4096, 0x02, 1024); - memset(expected + 4096, 0x02, 1024); - ret = do_write(fd, handle, buf + 4096, 4096, 1024); - assert(ret == 0); - ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); - assert(ret == 0); - assert(memcmp(buf, expected, sizeof(buf)) == 0); - - printf("Testing partial read of buffer\n"); - ret = do_read(fd, handle, buf, 512, 1024); - assert(ret == 0); - assert(memcmp(buf, expected + 512, 1024) == 0); - - close(fd); - - return 0; -} -- 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 ++- shared-core/drm.h | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) 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) { diff --git a/shared-core/drm.h b/shared-core/drm.h index 6d62af6a..1f49cbb0 100644 --- a/shared-core/drm.h +++ b/shared-core/drm.h @@ -968,7 +968,11 @@ struct drm_gem_alloc { * The (page-aligned) allocated size for the object will be returned. */ uint32_t size; - /** Returned handle for the object. */ + /** + * Returned handle for the object. + * + * Object handles are nonzero. + */ uint32_t handle; }; -- 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(+) 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(-) 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(-) 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(-) 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 ++++++++------ shared-core/drm.h | 28 +++++++++++++++++----------- shared-core/i915_drm.h | 21 ++++++++++++--------- 4 files changed, 40 insertions(+), 29 deletions(-) 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 */ diff --git a/shared-core/drm.h b/shared-core/drm.h index 1f49cbb0..90c23fa7 100644 --- a/shared-core/drm.h +++ b/shared-core/drm.h @@ -967,25 +967,28 @@ struct drm_gem_alloc { * * The (page-aligned) allocated size for the object will be returned. */ - uint32_t size; + uint64_t size; /** * Returned handle for the object. * * Object handles are nonzero. */ uint32_t handle; + uint32_t pad; }; struct drm_gem_unreference { /** Handle of the object to be unreferenced. */ uint32_t handle; + uint32_t pad; }; struct drm_gem_link { /** Handle for the object being given a name. */ uint32_t handle; + uint32_t pad; /** Requested file name to export the object under. */ - char *name; + uint64_t name_ptr; /* char *, but pointers are not 32/64 compatible */ /** Requested file mode to export the object under. */ mode_t mode; }; @@ -993,38 +996,41 @@ struct drm_gem_link { struct drm_gem_pread { /** Handle for the object being read. */ uint32_t handle; + uint32_t pad; /** Offset into the object to read from */ - off_t offset; + uint64_t offset; /** Length of data to read */ - size_t size; + uint64_t size; /** Pointer to write the data into. */ - void *data; + uint64_t data_ptr; /* void *, but pointers are not 32/64 compatible */ }; struct drm_gem_pwrite { /** Handle for the object being written to. */ uint32_t handle; + uint32_t pad; /** Offset into the object to write to */ - off_t offset; + uint64_t offset; /** Length of data to write */ - size_t size; + uint64_t size; /** Pointer to read the data from. */ - void *data; + uint64_t data_ptr; /* void *, but pointers are not 32/64 compatible */ }; struct drm_gem_mmap { /** Handle for the object being mapped. */ uint32_t handle; + uint32_t pad; /** Offset in the object to map. */ - off_t offset; + uint64_t offset; /** * Length of data to map. * * The value will be page-aligned. */ - size_t size; + uint64_t size; /** Returned pointer the data was mapped at */ - void *addr; + uint64_t addr_ptr; /* void *, but pointers are not 32/64 compatible */ }; /** diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 4d113e4c..91461c41 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -407,12 +407,12 @@ struct drm_i915_gem_init { * Beginning offset in the GTT to be managed by the DRM memory * manager. */ - off_t gtt_start; + uint64_t gtt_start; /** * Ending offset in the GTT to be managed by the DRM memory * manager. */ - off_t gtt_end; + uint64_t gtt_end; }; struct drm_i915_gem_relocation_entry { @@ -425,15 +425,15 @@ struct drm_i915_gem_relocation_entry { */ uint32_t target_handle; - /** Offset in the buffer the relocation entry will be written into */ - uint32_t offset; - /** * 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. @@ -442,7 +442,7 @@ struct drm_i915_gem_relocation_entry { * and writing the relocation. This value is written back out by * the execbuffer ioctl when the relocation is written. */ - uint32_t presumed_offset; + uint64_t presumed_offset; }; struct drm_i915_gem_validate_entry { @@ -457,8 +457,9 @@ struct drm_i915_gem_validate_entry { */ uint32_t buffer_offset; /** List of relocations to be performed on this buffer */ - struct drm_i915_gem_relocation_entry *relocs; + uint64_t relocs_ptr; /* struct drm_i915_gem_relocation_entry *relocs */ uint32_t relocation_count; + uint32_t pad; }; struct drm_i915_gem_execbuffer { @@ -470,7 +471,7 @@ struct drm_i915_gem_execbuffer { * 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; /* struct drm_i915_gem_validate_entry *buffers */ uint32_t buffer_count; /** Offset in the batchbuffer to start execution from. */ @@ -480,12 +481,13 @@ struct drm_i915_gem_execbuffer { uint32_t DR1; uint32_t DR4; uint32_t num_cliprects; - struct drm_clip_rect *cliprects; + uint64_t cliprects_ptr; /* struct drm_clip_rect *cliprects */ }; struct drm_i915_gem_pin { /** Handle of the buffer to be pinned. */ uint32_t handle; + uint32_t pad; /** Returned GTT offset of the buffer. */ uint64_t offset; @@ -494,6 +496,7 @@ struct drm_i915_gem_pin { struct drm_i915_gem_unpin { /** Handle of the buffer to be unpinned. */ uint32_t handle; + uint32_t pad; }; -- 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 ++++++++++----- shared-core/i915_drm.h | 16 +++++++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) 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); diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 91461c41..0c64e866 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -451,15 +451,18 @@ struct drm_i915_gem_validate_entry { * operation. */ uint32_t buffer_handle; + + /** List of relocations to be performed on this buffer */ + uint32_t relocation_count; + uint64_t relocs_ptr; /* struct drm_i915_gem_relocation_entry *relocs */ + + /** Required alignment in graphics aperture */ + uint64_t alignment; /** * Returned value of the updated offset of the buffer, for future * presumed_offset writes. */ - uint32_t buffer_offset; - /** List of relocations to be performed on this buffer */ - uint64_t relocs_ptr; /* struct drm_i915_gem_relocation_entry *relocs */ - uint32_t relocation_count; - uint32_t pad; + uint64_t buffer_offset; }; struct drm_i915_gem_execbuffer { @@ -488,6 +491,9 @@ 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; -- cgit v1.2.3 From 5b5b68ffd220f43d4da35ac475b8a3dd91ebbfa7 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 2 May 2008 10:34:46 -0700 Subject: Fix nouveau warning when returning pointers in uint64_t objects. --- shared-core/nouveau_state.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-core/nouveau_state.c b/shared-core/nouveau_state.c index 555955d4..9e79d3bc 100644 --- a/shared-core/nouveau_state.c +++ b/shared-core/nouveau_state.c @@ -614,7 +614,7 @@ int nouveau_ioctl_getparam(struct drm_device *dev, void *data, struct drm_file * break; case NOUVEAU_GETPARAM_PCI_PHYSICAL: if ( dev -> sg ) - getparam->value=(uint64_t) dev->sg->virtual; + getparam->value=(uint64_t) (uintptr_t) dev->sg->virtual; else { DRM_ERROR("Requested PCIGART address, while no PCIGART was created\n"); -- 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 +++++++++++++++++++++----------------------- shared-core/i915_drv.h | 4 ++-- 4 files changed, 55 insertions(+), 64 deletions(-) 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); } diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 801ac99e..0ebfedca 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -366,8 +366,8 @@ int i915_gem_pin_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int i915_gem_unpin_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); -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); +int i915_gem_init_object(struct drm_gem_object *obj); +void i915_gem_free_object(struct drm_gem_object *obj); #endif -- 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 +- shared-core/drm.h | 31 +++++--- 5 files changed, 233 insertions(+), 40 deletions(-) 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, diff --git a/shared-core/drm.h b/shared-core/drm.h index 90c23fa7..0e8da7b9 100644 --- a/shared-core/drm.h +++ b/shared-core/drm.h @@ -983,16 +983,6 @@ struct drm_gem_unreference { uint32_t pad; }; -struct drm_gem_link { - /** Handle for the object being given a name. */ - uint32_t handle; - uint32_t pad; - /** Requested file name to export the object under. */ - uint64_t name_ptr; /* char *, but pointers are not 32/64 compatible */ - /** Requested file mode to export the object under. */ - mode_t mode; -}; - struct drm_gem_pread { /** Handle for the object being read. */ uint32_t handle; @@ -1033,6 +1023,25 @@ struct drm_gem_mmap { uint64_t addr_ptr; /* void *, but pointers are not 32/64 compatible */ }; +struct drm_gem_name { + /** Handle for the object being named */ + uint32_t handle; + + /** Returned global name */ + uint32_t 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; +}; + /** * \name Ioctls Definitions */ @@ -1058,6 +1067,8 @@ struct drm_gem_mmap { #define DRM_IOCTL_GEM_PREAD DRM_IOW(0x0b, struct drm_gem_pread) #define DRM_IOCTL_GEM_PWRITE DRM_IOW(0x0c, struct drm_gem_pwrite) #define DRM_IOCTL_GEM_MMAP DRM_IOWR(0x0d, struct drm_gem_mmap) +#define DRM_IOCTL_GEM_NAME DRM_IOWR(0x0e, struct drm_gem_name) +#define DRM_IOCTL_GEM_OPEN DRM_IOWR(0x0f, struct drm_gem_open) #define DRM_IOCTL_SET_UNIQUE DRM_IOW( 0x10, struct drm_unique) #define DRM_IOCTL_AUTH_MAGIC DRM_IOW( 0x11, struct drm_auth) -- 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(-) 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(-) 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(-) 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(-) 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 +++++++++++++++++++++++++++++++++++++++++-------- shared-core/i915_dma.c | 6 +-- shared-core/i915_drv.h | 4 ++ 4 files changed, 107 insertions(+), 22 deletions(-) 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; } diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 73f843c8..4bf37325 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -458,9 +458,9 @@ static int i915_emit_cmds(struct drm_device *dev, int __user *buffer, return 0; } -static int i915_emit_box(struct drm_device * dev, - struct drm_clip_rect __user * boxes, - int i, int DR1, int DR4) +int i915_emit_box(struct drm_device * dev, + struct drm_clip_rect __user * boxes, + int i, int DR1, int DR4) { drm_i915_private_t *dev_priv = dev->dev_private; struct drm_clip_rect box; diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 0ebfedca..c6d86584 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -299,6 +299,10 @@ extern int i915_dispatch_batchbuffer(struct drm_device * dev, drm_i915_batchbuffer_t * batch); extern int i915_quiescent(struct drm_device *dev); +int i915_emit_box(struct drm_device * dev, + struct drm_clip_rect __user * boxes, + int i, int DR1, int DR4); + /* i915_irq.c */ extern int i915_irq_emit(struct drm_device *dev, void *data, struct drm_file *file_priv); -- 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 ++++++++++++++++++++++++++++++++--------------- shared-core/i915_dma.c | 1 + 2 files changed, 33 insertions(+), 15 deletions(-) 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, diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 4bf37325..f23e2a36 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -470,6 +470,7 @@ int i915_emit_box(struct drm_device * dev, return -EFAULT; } + DRM_INFO ("box %d,%d - %d,%d\n", box.x1, box.y1, box.x2, box.y2); if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) { DRM_ERROR("Bad box %d,%d..%d,%d\n", box.x1, box.y1, box.x2, box.y2); -- 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(-) 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(-) 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 +++++++++++++---------------- shared-core/i915_dma.c | 1 - 2 files changed, 13 insertions(+), 17 deletions(-) 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. diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index f23e2a36..4bf37325 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -470,7 +470,6 @@ int i915_emit_box(struct drm_device * dev, return -EFAULT; } - DRM_INFO ("box %d,%d - %d,%d\n", box.x1, box.y1, box.x2, box.y2); if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) { DRM_ERROR("Bad box %d,%d..%d,%d\n", box.x1, box.y1, box.x2, box.y2); -- 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(-) 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(-) 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 ed6657fa8e7977b19bb836782ac8e87f0f703cef Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 5 May 2008 22:09:34 -0700 Subject: Monitor ACTHD register while polling for idle ring. When batch buffers are executing, the ring may be stuck for a long time. Monitor the ACTHD pointer which will show if the execution engine is actually hung. --- shared-core/i915_dma.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 4bf37325..d881a231 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -41,10 +41,14 @@ int i915_wait_ring(struct drm_device * dev, int n, const char *caller) drm_i915_private_t *dev_priv = dev->dev_private; drm_i915_ring_buffer_t *ring = &(dev_priv->ring); u32 last_head = I915_READ(LP_RING + RING_HEAD) & HEAD_ADDR; + u32 acthd_reg = IS_I965G(dev) ? I965REG_ACTHD : I915REG_ACTHD; + u32 last_acthd = I915_READ(acthd_reg); + u32 acthd; int i; - for (i = 0; i < 10000; i++) { + for (i = 0; i < 100000; i++) { ring->head = I915_READ(LP_RING + RING_HEAD) & HEAD_ADDR; + acthd = I915_READ(acthd_reg); ring->space = ring->head - (ring->tail + 8); if (ring->space < 0) ring->space += ring->Size; @@ -54,8 +58,12 @@ int i915_wait_ring(struct drm_device * dev, int n, const char *caller) if (ring->head != last_head) i = 0; + if (acthd != last_acthd) + i = 0; + last_head = ring->head; - DRM_UDELAY(1); + last_acthd = acthd; + DRM_UDELAY(10); } return -EBUSY; @@ -714,9 +722,19 @@ void i915_dispatch_flip(struct drm_device * dev, int planes, int sync) int i915_quiescent(struct drm_device *dev) { drm_i915_private_t *dev_priv = dev->dev_private; + int ret; i915_kernel_lost_context(dev); - return i915_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__); + ret = i915_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__); + if (ret) + { + i915_kernel_lost_context (dev); + DRM_ERROR ("not quiescent head %08x tail %08x space %08x\n", + dev_priv->ring.head, + dev_priv->ring.tail, + dev_priv->ring.space); + } + return ret; } static int i915_flush_ioctl(struct drm_device *dev, void *data, -- 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 +++++++++++++++++++++++++++++++++++--------------- shared-core/i915_drv.h | 2 ++ 2 files changed, 64 insertions(+), 25 deletions(-) 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++) { diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index c6d86584..6c06ff5c 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -544,6 +544,8 @@ extern int i915_wait_ring(struct drm_device * dev, int n, const char *caller); #define I915REG_INT_MASK_R 0x020a8 #define I915REG_INT_ENABLE_R 0x020a0 #define I915REG_INSTPM 0x020c0 +#define I965REG_ACTHD 0x02074 +#define I915REG_ACTHD 0x020C8 #define PIPEADSL 0x70000 #define PIPEBDSL 0x71000 -- 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(-) 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(+) 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 ++++++++++++++++++++++++++++++++++++------------- shared-core/i915_dma.c | 2 + shared-core/i915_drv.h | 10 +++ shared-core/i915_irq.c | 2 +- 4 files changed, 143 insertions(+), 46 deletions(-) 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; } diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index d881a231..0601b89c 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1051,6 +1051,8 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags) ret = drm_addmap(dev, base, size, _DRM_REGISTERS, _DRM_KERNEL | _DRM_DRIVER, &dev_priv->mmio_map); + INIT_LIST_HEAD(&dev_priv->mm.gtt_lru); + #ifdef __linux__ #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) intel_init_chipset_flush_compat(dev); diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 6c06ff5c..bd6fa188 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -244,6 +244,8 @@ typedef struct drm_i915_private { struct { struct drm_memrange gtt_space; + /** LRU List of unpinned objects in the GTT. */ + struct list_head gtt_lru; } mm; } drm_i915_private_t; @@ -256,8 +258,12 @@ enum intel_chip_family { /** driver private structure attached to each drm_gem_object */ struct drm_i915_gem_object { + struct drm_gem_object *obj; + /** Current space allocated to this object in the GTT, if any. */ struct drm_memrange_node *gtt_space; + /** This object's place on the GTT LRU list */ + struct list_head gtt_lru_entry; /** AGP memory structure for our GTT binding. */ DRM_AGP_MEM *agp_mem; @@ -276,6 +282,9 @@ struct drm_i915_gem_object { /** How many users have pinned this object in GTT space */ int pin_count; + + /** Breadcrumb of last rendering to the buffer. */ + uint32_t last_rendering_cookie; }; extern struct drm_ioctl_desc i915_ioctls[]; @@ -318,6 +327,7 @@ extern int i915_vblank_pipe_set(struct drm_device *dev, void *data, extern int i915_vblank_pipe_get(struct drm_device *dev, void *data, struct drm_file *file_priv); extern int i915_emit_irq(struct drm_device * dev); +extern int i915_wait_irq(struct drm_device * dev, int irq_nr); extern int i915_enable_vblank(struct drm_device *dev, int crtc); extern void i915_disable_vblank(struct drm_device *dev, int crtc); extern u32 i915_get_vblank_counter(struct drm_device *dev, int crtc); diff --git a/shared-core/i915_irq.c b/shared-core/i915_irq.c index 785008d4..b17e2408 100644 --- a/shared-core/i915_irq.c +++ b/shared-core/i915_irq.c @@ -545,7 +545,7 @@ void i915_user_irq_off(drm_i915_private_t *dev_priv) } -static int i915_wait_irq(struct drm_device * dev, int irq_nr) +int i915_wait_irq(struct drm_device * dev, int irq_nr) { drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; int ret = 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 ++++++++++++++++++++++++++++++++++++++ shared-core/drm.h | 29 ++++++++++++++++++++++------- shared-core/i915_drm.h | 27 +++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 8 deletions(-) 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); diff --git a/shared-core/drm.h b/shared-core/drm.h index 0e8da7b9..f1430f59 100644 --- a/shared-core/drm.h +++ b/shared-core/drm.h @@ -1042,6 +1042,19 @@ struct drm_gem_open { uint64_t size; }; +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; +}; + +#define DRM_GEM_DOMAIN_CPU 0x00000001 + /** * \name Ioctls Definitions */ @@ -1062,13 +1075,6 @@ struct drm_gem_open { #define DRM_IOCTL_GET_STATS DRM_IOR( 0x06, struct drm_stats) #define DRM_IOCTL_SET_VERSION DRM_IOWR(0x07, struct drm_set_version) #define DRM_IOCTL_MODESET_CTL DRM_IOW(0x08, struct drm_modeset_ctl) -#define DRM_IOCTL_GEM_ALLOC DRM_IOWR(0x09, struct drm_gem_alloc) -#define DRM_IOCTL_GEM_UNREFERENCE DRM_IOW(0x0a, struct drm_gem_unreference) -#define DRM_IOCTL_GEM_PREAD DRM_IOW(0x0b, struct drm_gem_pread) -#define DRM_IOCTL_GEM_PWRITE DRM_IOW(0x0c, struct drm_gem_pwrite) -#define DRM_IOCTL_GEM_MMAP DRM_IOWR(0x0d, struct drm_gem_mmap) -#define DRM_IOCTL_GEM_NAME DRM_IOWR(0x0e, struct drm_gem_name) -#define DRM_IOCTL_GEM_OPEN DRM_IOWR(0x0f, struct drm_gem_open) #define DRM_IOCTL_SET_UNIQUE DRM_IOW( 0x10, struct drm_unique) #define DRM_IOCTL_AUTH_MAGIC DRM_IOW( 0x11, struct drm_auth) @@ -1117,6 +1123,15 @@ struct drm_gem_open { #define DRM_IOCTL_UPDATE_DRAW DRM_IOW(0x3f, struct drm_update_draw) +#define DRM_IOCTL_GEM_ALLOC DRM_IOWR(0x09, struct drm_gem_alloc) +#define DRM_IOCTL_GEM_UNREFERENCE DRM_IOW (0x0a, struct drm_gem_unreference) +#define DRM_IOCTL_GEM_PREAD DRM_IOW (0x0b, struct drm_gem_pread) +#define DRM_IOCTL_GEM_PWRITE DRM_IOW (0x0c, struct drm_gem_pwrite) +#define DRM_IOCTL_GEM_MMAP DRM_IOWR(0x0d, struct drm_gem_mmap) +#define DRM_IOCTL_GEM_NAME DRM_IOWR(0x0e, struct drm_gem_name) +#define DRM_IOCTL_GEM_OPEN DRM_IOWR(0x0f, struct drm_gem_open) +#define DRM_IOCTL_GEM_SET_DOMAIN DRM_IOW (0xb7, struct drm_gem_set_domain) + #define DRM_IOCTL_MM_INIT DRM_IOWR(0xc0, struct drm_mm_init_arg) #define DRM_IOCTL_MM_TAKEDOWN DRM_IOWR(0xc1, struct drm_mm_type_arg) #define DRM_IOCTL_MM_LOCK DRM_IOWR(0xc2, struct drm_mm_type_arg) diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 0c64e866..302fc646 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -445,6 +445,28 @@ struct drm_i915_gem_relocation_entry { uint64_t presumed_offset; }; +/** + * Intel memory domains + * + * Most of these just align with the various caches in + * the system and are used to flush and invalidate as + * objects end up cached in different domains. + * + * STOLEN is a domain for the stolen memory portion of the + * address space; those pages are accessible only through the + * GTT and, hence, look a lot like VRAM on a discrete card. + * We'll allow programs to move objects into stolen memory + * mostly as a way to demonstrate the VRAM capabilities of this + * API + */ + +/* 0x00000001 is DRM_GEM_DOMAIN_CPU */ +#define DRM_GEM_DOMAIN_I915_RENDER 0x00000002 /* Render cache, used by 2D and 3D drawing */ +#define DRM_GEM_DOMAIN_I915_SAMPLER 0x00000004 /* Sampler cache, used by texture engine */ +#define DRM_GEM_DOMAIN_I915_COMMAND 0x00000008 /* Command queue, used to load batch buffers */ +#define DRM_GEM_DOMAIN_I915_INSTRUCTION 0x00000010 /* Instruction cache, used by shader programs */ +#define DRM_GEM_DOMAIN_I915_STOLEN 0x00000020 /* Stolen memory, needed by some objects */ + struct drm_i915_gem_validate_entry { /** * User's handle for a buffer to be bound into the GTT for this @@ -458,6 +480,11 @@ struct drm_i915_gem_validate_entry { /** Required alignment in graphics aperture */ uint64_t alignment; + + /** Memory domains used in this execbuffer run */ + uint32_t read_domains; + uint32_t write_domain; + /** * Returned value of the updated offset of the buffer, for future * presumed_offset writes. -- 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(-) 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 +++++++++++++++++++++++++++++++++++++++----------- shared-core/i915_drv.h | 1 + 2 files changed, 50 insertions(+), 13 deletions(-) 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; } diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index bd6fa188..daa77f72 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -262,6 +262,7 @@ struct drm_i915_gem_object { /** Current space allocated to this object in the GTT, if any. */ struct drm_memrange_node *gtt_space; + /** This object's place on the GTT LRU list */ struct list_head gtt_lru_entry; -- 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 +++++++++++++++++++++++++++++++++++++--- shared-core/i915_drm.h | 1 + 2 files changed, 38 insertions(+), 3 deletions(-) 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; } diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 302fc646..8c3cd646 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -466,6 +466,7 @@ struct drm_i915_gem_relocation_entry { #define DRM_GEM_DOMAIN_I915_COMMAND 0x00000008 /* Command queue, used to load batch buffers */ #define DRM_GEM_DOMAIN_I915_INSTRUCTION 0x00000010 /* Instruction cache, used by shader programs */ #define DRM_GEM_DOMAIN_I915_STOLEN 0x00000020 /* Stolen memory, needed by some objects */ +#define DRM_GEM_DOMAIN_I915_VERTEX 0x00000040 /* Vertex address cache */ struct drm_i915_gem_validate_entry { /** -- 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(+) 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(-) 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 effc6d998f080ba6f9c81d1b4b0e75a42be0238e Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Wed, 7 May 2008 16:00:58 -0700 Subject: GEM: fix testcases for new ioctl args. --- tests/gem_mmap.c | 14 ++++++++------ tests/gem_readwrite.c | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/gem_mmap.c b/tests/gem_mmap.c index 2cdc9343..4a7d6ecc 100644 --- a/tests/gem_mmap.c +++ b/tests/gem_mmap.c @@ -48,7 +48,7 @@ int do_read(int fd, int handle, void *buf, int offset, int size) memset(&read, 0, sizeof(read)); read.handle = handle; - read.data = buf; + read.data_ptr = (uintptr_t)buf; read.size = size; read.offset = offset; @@ -61,7 +61,7 @@ int do_write(int fd, int handle, void *buf, int offset, int size) memset(&write, 0, sizeof(write)); write.handle = handle; - write.data = buf; + write.data_ptr = (uintptr_t)buf; write.size = size; write.offset = offset; @@ -76,6 +76,7 @@ int main(int argc, char **argv) struct drm_gem_unreference unref; uint8_t expected[OBJECT_SIZE]; uint8_t buf[OBJECT_SIZE]; + uint8_t *addr; int ret; int handle; @@ -101,10 +102,11 @@ int main(int argc, char **argv) mmap.size = OBJECT_SIZE; ret = ioctl(fd, DRM_IOCTL_GEM_MMAP, &mmap); assert(ret == 0); + addr = (uint8_t *)(uintptr_t)mmap.addr_ptr; printf("Testing contents of newly allocated object.\n"); memset(expected, 0, sizeof(expected)); - assert(memcmp(mmap.addr, expected, sizeof(expected)) == 0); + assert(memcmp(addr, expected, sizeof(expected)) == 0); printf("Testing coherency of writes and mmap reads.\n"); memset(buf, 0, sizeof(buf)); @@ -112,16 +114,16 @@ int main(int argc, char **argv) memset(expected + 1024, 0x01, 1024); ret = do_write(fd, handle, buf, 0, OBJECT_SIZE); assert(ret == 0); - assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); + assert(memcmp(buf, addr, sizeof(buf)) == 0); printf("Testing that mapping stays after unreference\n"); unref.handle = handle; ret = ioctl(fd, DRM_IOCTL_GEM_UNREFERENCE, &unref); assert(ret == 0); - assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0); + assert(memcmp(buf, addr, sizeof(buf)) == 0); printf("Testing unmapping\n"); - munmap(mmap.addr, OBJECT_SIZE); + munmap(addr, OBJECT_SIZE); close(fd); diff --git a/tests/gem_readwrite.c b/tests/gem_readwrite.c index 5065732d..1cc8a3e2 100644 --- a/tests/gem_readwrite.c +++ b/tests/gem_readwrite.c @@ -48,7 +48,7 @@ int do_read(int fd, int handle, void *buf, int offset, int size) memset(&read, 0, sizeof(read)); read.handle = handle; - read.data = buf; + read.data_ptr = (uintptr_t)buf; read.size = size; read.offset = offset; @@ -61,7 +61,7 @@ int do_write(int fd, int handle, void *buf, int offset, int size) memset(&write, 0, sizeof(write)); write.handle = handle; - write.data = buf; + write.data_ptr = (uintptr_t)buf; write.size = size; write.offset = offset; -- 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 +++++++++++++++++++++++++++++++++++-------------- shared-core/i915_drm.h | 28 +++--- shared-core/i915_drv.h | 3 + 7 files changed, 238 insertions(+), 83 deletions(-) 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; +} diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 8c3cd646..d71447e6 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -443,6 +443,20 @@ struct drm_i915_gem_relocation_entry { * 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; }; /** @@ -451,13 +465,6 @@ struct drm_i915_gem_relocation_entry { * Most of these just align with the various caches in * the system and are used to flush and invalidate as * objects end up cached in different domains. - * - * STOLEN is a domain for the stolen memory portion of the - * address space; those pages are accessible only through the - * GTT and, hence, look a lot like VRAM on a discrete card. - * We'll allow programs to move objects into stolen memory - * mostly as a way to demonstrate the VRAM capabilities of this - * API */ /* 0x00000001 is DRM_GEM_DOMAIN_CPU */ @@ -465,8 +472,7 @@ struct drm_i915_gem_relocation_entry { #define DRM_GEM_DOMAIN_I915_SAMPLER 0x00000004 /* Sampler cache, used by texture engine */ #define DRM_GEM_DOMAIN_I915_COMMAND 0x00000008 /* Command queue, used to load batch buffers */ #define DRM_GEM_DOMAIN_I915_INSTRUCTION 0x00000010 /* Instruction cache, used by shader programs */ -#define DRM_GEM_DOMAIN_I915_STOLEN 0x00000020 /* Stolen memory, needed by some objects */ -#define DRM_GEM_DOMAIN_I915_VERTEX 0x00000040 /* Vertex address cache */ +#define DRM_GEM_DOMAIN_I915_VERTEX 0x00000020 /* Vertex address cache */ struct drm_i915_gem_validate_entry { /** @@ -482,10 +488,6 @@ struct drm_i915_gem_validate_entry { /** Required alignment in graphics aperture */ uint64_t alignment; - /** Memory domains used in this execbuffer run */ - uint32_t read_domains; - uint32_t write_domain; - /** * Returned value of the updated offset of the buffer, for future * presumed_offset writes. diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index daa77f72..96257ab6 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -383,6 +383,9 @@ int i915_gem_unpin_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int i915_gem_init_object(struct drm_gem_object *obj); 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); #endif -- 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(+) 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(-) 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(-) 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 ++++++++++---------- shared-core/drm.h | 14 +++++++------- 4 files changed, 25 insertions(+), 25 deletions(-) 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; diff --git a/shared-core/drm.h b/shared-core/drm.h index f1430f59..7b69a634 100644 --- a/shared-core/drm.h +++ b/shared-core/drm.h @@ -961,7 +961,7 @@ struct drm_mm_info_arg { }; -struct drm_gem_alloc { +struct drm_gem_create { /** * Requested size for the object. * @@ -977,8 +977,8 @@ struct drm_gem_alloc { uint32_t pad; }; -struct drm_gem_unreference { - /** Handle of the object to be unreferenced. */ +struct drm_gem_close { + /** Handle of the object to be closed. */ uint32_t handle; uint32_t pad; }; @@ -1023,7 +1023,7 @@ struct drm_gem_mmap { uint64_t addr_ptr; /* void *, but pointers are not 32/64 compatible */ }; -struct drm_gem_name { +struct drm_gem_flink { /** Handle for the object being named */ uint32_t handle; @@ -1123,12 +1123,12 @@ struct drm_gem_set_domain { #define DRM_IOCTL_UPDATE_DRAW DRM_IOW(0x3f, struct drm_update_draw) -#define DRM_IOCTL_GEM_ALLOC DRM_IOWR(0x09, struct drm_gem_alloc) -#define DRM_IOCTL_GEM_UNREFERENCE DRM_IOW (0x0a, struct drm_gem_unreference) +#define DRM_IOCTL_GEM_CREATE DRM_IOWR(0x09, struct drm_gem_create) +#define DRM_IOCTL_GEM_CLOSE DRM_IOW (0x0a, struct drm_gem_close) #define DRM_IOCTL_GEM_PREAD DRM_IOW (0x0b, struct drm_gem_pread) #define DRM_IOCTL_GEM_PWRITE DRM_IOW (0x0c, struct drm_gem_pwrite) #define DRM_IOCTL_GEM_MMAP DRM_IOWR(0x0d, struct drm_gem_mmap) -#define DRM_IOCTL_GEM_NAME DRM_IOWR(0x0e, struct drm_gem_name) +#define DRM_IOCTL_GEM_FLINK DRM_IOWR(0x0e, struct drm_gem_flink) #define DRM_IOCTL_GEM_OPEN DRM_IOWR(0x0f, struct drm_gem_open) #define DRM_IOCTL_GEM_SET_DOMAIN DRM_IOW (0xb7, struct drm_gem_set_domain) -- 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(-) 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(+) 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 ++++++++++++++++++++++++++++++++++++------------- shared-core/i915_dma.c | 2 + shared-core/i915_drv.h | 9 +++- 3 files changed, 106 insertions(+), 35 deletions(-) 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); + } +} diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 0601b89c..24f361e0 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1052,6 +1052,7 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags) _DRM_KERNEL | _DRM_DRIVER, &dev_priv->mmio_map); INIT_LIST_HEAD(&dev_priv->mm.gtt_lru); + INIT_LIST_HEAD(&dev_priv->mm.execution_list); #ifdef __linux__ #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) @@ -1093,6 +1094,7 @@ void i915_driver_lastclose(struct drm_device * dev) dev_priv->val_bufs = NULL; } #endif + i915_gem_lastclose(dev); if (drm_getsarea(dev) && dev_priv->sarea_priv) i915_do_cleanup_pageflip(dev); diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 96257ab6..413fca89 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -244,7 +244,12 @@ typedef struct drm_i915_private { struct { struct drm_memrange gtt_space; - /** LRU List of unpinned objects in the GTT. */ + /** + * List of objects currently involved in rendering from the + * ringbuffer. + */ + struct list_head execution_list; + /** LRU List of non-executing objects still in the GTT. */ struct list_head gtt_lru; } mm; } drm_i915_private_t; @@ -386,7 +391,7 @@ 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); - +void i915_gem_lastclose(struct drm_device *dev); #endif #ifdef __linux__ -- 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(-) 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 ++++++------- shared-core/i915_drm.h | 8 ++++---- 2 files changed, 10 insertions(+), 11 deletions(-) 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], diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index d71447e6..5f7cefeb 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -474,12 +474,12 @@ struct drm_i915_gem_relocation_entry { #define DRM_GEM_DOMAIN_I915_INSTRUCTION 0x00000010 /* Instruction cache, used by shader programs */ #define DRM_GEM_DOMAIN_I915_VERTEX 0x00000020 /* Vertex address cache */ -struct drm_i915_gem_validate_entry { +struct drm_i915_gem_exec_object { /** * User's handle for a buffer to be bound into the GTT for this * operation. */ - uint32_t buffer_handle; + uint32_t handle; /** List of relocations to be performed on this buffer */ uint32_t relocation_count; @@ -489,10 +489,10 @@ struct drm_i915_gem_validate_entry { uint64_t alignment; /** - * Returned value of the updated offset of the buffer, for future + * Returned value of the updated offset of the object, for future * presumed_offset writes. */ - uint64_t buffer_offset; + uint64_t offset; }; struct drm_i915_gem_execbuffer { -- 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 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(-) 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 +++++++++++++++++++++++++++++++++++--------------- shared-core/i915_drv.h | 2 ++ 5 files changed, 100 insertions(+), 27 deletions(-) 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) { diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 413fca89..78c5d3df 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -391,6 +391,8 @@ 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); +int i915_gem_flush_pwrite(struct drm_gem_object *obj, + uint64_t offset, uint64_t size); void i915_gem_lastclose(struct drm_device *dev); #endif -- 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(-) 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(-) 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 7e7ea313c4a38370194290d05697bdb6a746669d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 12 May 2008 13:03:16 -0700 Subject: [intel] When polling for ring space, sleep for a lot longer (10ms) If the ring is full, the engine will surely be running for more than 10ms. --- shared-core/i915_dma.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 24f361e0..cf2dfeb6 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -63,7 +63,7 @@ int i915_wait_ring(struct drm_device * dev, int n, const char *caller) last_head = ring->head; last_acthd = acthd; - DRM_UDELAY(10); + msleep_interruptible (10); } return -EBUSY; -- 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(-) 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 ++++++++-------- shared-core/i915_drm.h | 7 ++++--- 2 files changed, 12 insertions(+), 11 deletions(-) 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 diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 5f7cefeb..b50471f3 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -420,8 +420,9 @@ 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. + * 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; @@ -497,7 +498,7 @@ struct drm_i915_gem_exec_object { struct drm_i915_gem_execbuffer { /** - * List of buffers to be validated wit their relocations to be + * List of buffers to be validated with their relocations to be * performend on them. * * These buffers must be listed in an order such that all relocations -- cgit v1.2.3 From 6e46a3c762919af05fcc6a08542faa7d185487a1 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 12 May 2008 15:42:20 -0700 Subject: [GEM] Update testcases for new API. --- tests/gem_basic.c | 46 ++++++++++++++++++++++------------------------ tests/gem_mmap.c | 20 ++++++++++---------- tests/gem_readwrite.c | 12 ++++++------ 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/tests/gem_basic.c b/tests/gem_basic.c index 6e1f3ddb..8b8b63d0 100644 --- a/tests/gem_basic.c +++ b/tests/gem_basic.c @@ -36,48 +36,48 @@ #include "drm.h" static void -test_bad_unref(int fd) +test_bad_close(int fd) { - struct drm_gem_unreference unref; + struct drm_gem_close close; int ret; - printf("Testing error return on bad unreference ioctl.\n"); + printf("Testing error return on bad close ioctl.\n"); - unref.handle = 0x10101010; - ret = ioctl(fd, DRM_IOCTL_GEM_UNREFERENCE, &unref); + close.handle = 0x10101010; + ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close); assert(ret == -1 && errno == EINVAL); } static void -test_alloc_unref(int fd) +test_create_close(int fd) { - struct drm_gem_alloc alloc; - struct drm_gem_unreference unref; + struct drm_gem_create create; + struct drm_gem_close close; int ret; - printf("Testing allocating and unreferencing an object.\n"); + printf("Testing creating and closing an object.\n"); - memset(&alloc, 0, sizeof(alloc)); - alloc.size = 16 * 1024; - ret = ioctl(fd, DRM_IOCTL_GEM_ALLOC, &alloc); + memset(&create, 0, sizeof(create)); + create.size = 16 * 1024; + ret = ioctl(fd, DRM_IOCTL_GEM_CREATE, &create); assert(ret == 0); - unref.handle = alloc.handle; - ret = ioctl(fd, DRM_IOCTL_GEM_UNREFERENCE, &unref); + close.handle = create.handle; + ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close); } static void -test_alloc_close(int fd) +test_create_fd_close(int fd) { - struct drm_gem_alloc alloc; + struct drm_gem_create create; int ret; printf("Testing closing with an object allocated.\n"); - memset(&alloc, 0, sizeof(alloc)); - alloc.size = 16 * 1024; - ret = ioctl(fd, DRM_IOCTL_GEM_ALLOC, &alloc); + memset(&create, 0, sizeof(create)); + create.size = 16 * 1024; + ret = ioctl(fd, DRM_IOCTL_GEM_CREATE, &create); assert(ret == 0); close(fd); @@ -89,11 +89,9 @@ int main(int argc, char **argv) fd = drm_open_any(); - test_bad_unref(fd); - test_alloc_unref(fd); - test_alloc_close(fd); - - close(fd); + test_bad_close(fd); + test_create_close(fd); + test_create_fd_close(fd); return 0; } diff --git a/tests/gem_mmap.c b/tests/gem_mmap.c index 4a7d6ecc..3f8e27a0 100644 --- a/tests/gem_mmap.c +++ b/tests/gem_mmap.c @@ -71,9 +71,9 @@ int do_write(int fd, int handle, void *buf, int offset, int size) int main(int argc, char **argv) { int fd; - struct drm_gem_alloc alloc; + struct drm_gem_create create; struct drm_gem_mmap mmap; - struct drm_gem_unreference unref; + struct drm_gem_close unref; uint8_t expected[OBJECT_SIZE]; uint8_t buf[OBJECT_SIZE]; uint8_t *addr; @@ -90,13 +90,13 @@ int main(int argc, char **argv) ret = ioctl(fd, DRM_IOCTL_GEM_MMAP, &mmap); assert(ret == -1 && errno == EINVAL); - memset(&alloc, 0, sizeof(alloc)); - alloc.size = OBJECT_SIZE; - ret = ioctl(fd, DRM_IOCTL_GEM_ALLOC, &alloc); + memset(&create, 0, sizeof(create)); + create.size = OBJECT_SIZE; + ret = ioctl(fd, DRM_IOCTL_GEM_CREATE, &create); assert(ret == 0); - handle = alloc.handle; + handle = create.handle; - printf("Testing mmaping of newly allocated object.\n"); + printf("Testing mmaping of newly created object.\n"); mmap.handle = handle; mmap.offset = 0; mmap.size = OBJECT_SIZE; @@ -104,7 +104,7 @@ int main(int argc, char **argv) assert(ret == 0); addr = (uint8_t *)(uintptr_t)mmap.addr_ptr; - printf("Testing contents of newly allocated object.\n"); + printf("Testing contents of newly created object.\n"); memset(expected, 0, sizeof(expected)); assert(memcmp(addr, expected, sizeof(expected)) == 0); @@ -116,9 +116,9 @@ int main(int argc, char **argv) assert(ret == 0); assert(memcmp(buf, addr, sizeof(buf)) == 0); - printf("Testing that mapping stays after unreference\n"); + printf("Testing that mapping stays after close\n"); unref.handle = handle; - ret = ioctl(fd, DRM_IOCTL_GEM_UNREFERENCE, &unref); + ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &unref); assert(ret == 0); assert(memcmp(buf, addr, sizeof(buf)) == 0); diff --git a/tests/gem_readwrite.c b/tests/gem_readwrite.c index 1cc8a3e2..a48f9847 100644 --- a/tests/gem_readwrite.c +++ b/tests/gem_readwrite.c @@ -71,7 +71,7 @@ int do_write(int fd, int handle, void *buf, int offset, int size) int main(int argc, char **argv) { int fd; - struct drm_gem_alloc alloc; + struct drm_gem_create create; uint8_t expected[OBJECT_SIZE]; uint8_t buf[OBJECT_SIZE]; int ret; @@ -79,13 +79,13 @@ int main(int argc, char **argv) fd = drm_open_any(); - memset(&alloc, 0, sizeof(alloc)); - alloc.size = OBJECT_SIZE; - ret = ioctl(fd, DRM_IOCTL_GEM_ALLOC, &alloc); + memset(&create, 0, sizeof(create)); + create.size = OBJECT_SIZE; + ret = ioctl(fd, DRM_IOCTL_GEM_CREATE, &create); assert(ret == 0); - handle = alloc.handle; + handle = create.handle; - printf("Testing contents of newly allocated object.\n"); + printf("Testing contents of newly created object.\n"); ret = do_read(fd, handle, buf, 0, OBJECT_SIZE); assert(ret == 0); memset(&expected, 0, sizeof(expected)); -- 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(-) 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 +++++++++++++++++++++++++------------------------- shared-core/i915_dma.c | 4 ++-- shared-core/i915_drv.h | 10 +++++----- 3 files changed, 34 insertions(+), 34 deletions(-) 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); diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index cf2dfeb6..0e832057 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1051,8 +1051,8 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags) ret = drm_addmap(dev, base, size, _DRM_REGISTERS, _DRM_KERNEL | _DRM_DRIVER, &dev_priv->mmio_map); - INIT_LIST_HEAD(&dev_priv->mm.gtt_lru); - INIT_LIST_HEAD(&dev_priv->mm.execution_list); + INIT_LIST_HEAD(&dev_priv->mm.active_list); + INIT_LIST_HEAD(&dev_priv->mm.inactive_list); #ifdef __linux__ #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 78c5d3df..51c6bb59 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -248,9 +248,9 @@ typedef struct drm_i915_private { * List of objects currently involved in rendering from the * ringbuffer. */ - struct list_head execution_list; + struct list_head active_list; /** LRU List of non-executing objects still in the GTT. */ - struct list_head gtt_lru; + struct list_head inactive_list; } mm; } drm_i915_private_t; @@ -267,9 +267,9 @@ struct drm_i915_gem_object { /** Current space allocated to this object in the GTT, if any. */ struct drm_memrange_node *gtt_space; - - /** This object's place on the GTT LRU list */ - struct list_head gtt_lru_entry; + + /** This object's place on the active or inactive lists */ + struct list_head list; /** AGP memory structure for our GTT binding. */ DRM_AGP_MEM *agp_mem; -- 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 ++++++++++++++++++++++++++++++++---- shared-core/i915_drv.h | 6 +++--- 4 files changed, 62 insertions(+), 8 deletions(-) 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); } diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 51c6bb59..296e1823 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -388,9 +388,9 @@ int i915_gem_unpin_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int i915_gem_init_object(struct drm_gem_object *obj); 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); +int i915_gem_set_domain(struct drm_gem_object *obj, + uint32_t read_domains, + uint32_t write_domain); int i915_gem_flush_pwrite(struct drm_gem_object *obj, uint64_t offset, uint64_t size); void i915_gem_lastclose(struct drm_device *dev); -- 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 +++++++++++++++++++++++++++----------------------- shared-core/i915_drv.h | 18 +++++++++++- 2 files changed, 61 insertions(+), 37 deletions(-) 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); } diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 296e1823..870726c5 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -247,9 +247,18 @@ typedef struct drm_i915_private { /** * List of objects currently involved in rendering from the * ringbuffer. + * + * A reference is held on the buffer while on this list. */ struct list_head active_list; - /** LRU List of non-executing objects still in the GTT. */ + /** + * LRU List of non-executing objects still in the GTT. + * There may still be dirty cachelines that need to be flushed + * before unbind. + * A reference is not held on the buffer while on this list, + * as merely being GTT-bound shouldn't prevent its being + * freed, and we'll pull it off the list in the free path. + */ struct list_head inactive_list; } mm; } drm_i915_private_t; @@ -271,6 +280,13 @@ struct drm_i915_gem_object { /** This object's place on the active or inactive lists */ struct list_head list; + /** + * This is set if the object is on the active list + * (has pending rendering), and is not set if it's on inactive (ready + * to be unbound). + */ + int active; + /** AGP memory structure for our GTT binding. */ DRM_AGP_MEM *agp_mem; -- 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 ++++++++++++++-------------- shared-core/i915_drv.h | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) 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 diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 870726c5..79c607bc 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -306,7 +306,7 @@ struct drm_i915_gem_object { int pin_count; /** Breadcrumb of last rendering to the buffer. */ - uint32_t last_rendering_cookie; + uint32_t last_rendering_seqno; }; extern struct drm_ioctl_desc i915_ioctls[]; -- 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 +++++++++++++++++++++++++++++++++++++++++++++++-- shared-core/i915_dma.c | 4 +- shared-core/i915_drv.h | 45 ++++++++++++++++++ 3 files changed, 168 insertions(+), 4 deletions(-) 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]; diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 0e832057..30ba8c65 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -524,7 +524,7 @@ void i915_emit_breadcrumb(struct drm_device *dev) BEGIN_LP_RING(4); OUT_RING(CMD_STORE_DWORD_IDX); - OUT_RING(20); + OUT_RING(5 << STORE_DWORD_INDEX_SHIFT); OUT_RING(dev_priv->counter); OUT_RING(0); ADVANCE_LP_RING(); @@ -1053,6 +1053,8 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags) INIT_LIST_HEAD(&dev_priv->mm.active_list); INIT_LIST_HEAD(&dev_priv->mm.inactive_list); + INIT_LIST_HEAD(&dev_priv->mm.request_list); + dev_priv->mm.next_gem_seqno = 1; #ifdef __linux__ #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 79c607bc..ef41b433 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -260,6 +260,14 @@ typedef struct drm_i915_private { * freed, and we'll pull it off the list in the free path. */ struct list_head inactive_list; + + /** + * List of breadcrumbs associated with GPU requests currently + * outstanding. + */ + struct list_head request_list; + + uint32_t next_gem_seqno; } mm; } drm_i915_private_t; @@ -309,6 +317,23 @@ struct drm_i915_gem_object { uint32_t last_rendering_seqno; }; +/** + * Request queue structure. + * + * The request queue allows us to note sequence numbers that have been emitted + * and may be associated with active buffers to be retired. + * + * By keeping this list, we can avoid having to do questionable + * sequence-number comparisons on buffer last_rendering_seqnos, and associate + * an emission time with seqnos for tracking how far ahead of the GPU we are. + */ +struct drm_i915_gem_request { + /** GEM sequence number associated with this request. */ + uint32_t seqno; + + struct list_head list; +}; + extern struct drm_ioctl_desc i915_ioctls[]; extern int i915_max_ioctl; @@ -506,7 +531,12 @@ extern int i915_wait_ring(struct drm_device * dev, int n, const char *caller); #define GFX_OP_BREAKPOINT_INTERRUPT ((0<<29)|(1<<23)) #define CMD_REPORT_HEAD (7<<23) #define CMD_STORE_DWORD_IMM ((0x20<<23) | (0x1 << 22) | 0x1) +/** + * Stores a 32-bit integer to the status page at the dword index given. + */ #define CMD_STORE_DWORD_IDX ((0x21<<23) | 0x1) +# define STORE_DWORD_INDEX_SHIFT 2 + #define CMD_OP_BATCH_BUFFER ((0x0<<29)|(0x30<<23)|0x1) #define CMD_MI_FLUSH (0x04 << 23) @@ -855,7 +885,22 @@ extern int i915_wait_ring(struct drm_device * dev, int n, const char *caller); #define BREADCRUMB_MASK ((1U << BREADCRUMB_BITS) - 1) #define READ_BREADCRUMB(dev_priv) (((volatile u32*)(dev_priv->hw_status_page))[5]) + +/** + * Reads a dword out of the status page, which is written to from the command + * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or + * MI_STORE_DATA_IMM. + * + * The following dwords have a reserved meaning: + * 0: ISR copy, updated when an ISR bit not set in the HWSTAM changes. + * 4: ring 0 head pointer + * 5: ring 1 head pointer (915-class) + * 6: ring 2 head pointer (915-class) + * + * The area from dword 0x10 to 0x3ff is available for driver usage. + */ #define READ_HWSP(dev_priv, reg) (((volatile u32*)(dev_priv->hw_status_page))[reg]) +#define I915_GEM_HWS_INDEX 0x10 #define BLC_PWM_CTL 0x61254 #define BACKLIGHT_MODULATION_FREQ_SHIFT (17) -- 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(+) 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(+) 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(-) 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 +++++++++++++++++++++++++++---------------------- shared-core/i915_drv.h | 3 +++ 2 files changed, 30 insertions(+), 22 deletions(-) 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; diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index ef41b433..e3f280d5 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -331,6 +331,9 @@ struct drm_i915_gem_request { /** GEM sequence number associated with this request. */ uint32_t seqno; + /** Time at which this request was emitted, in jiffies. */ + unsigned long emitted_jiffies; + struct list_head list; }; -- 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 ++++++++++++++++++++++++++++++++++++------------- shared-core/i915_dma.c | 4 ++ shared-core/i915_drv.h | 30 ++++++-- shared-core/i915_irq.c | 23 ++++++ 4 files changed, 188 insertions(+), 55 deletions(-) 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]; diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 30ba8c65..fa73cd29 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1043,6 +1043,7 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags) memset(dev_priv, 0, sizeof(drm_i915_private_t)); dev->dev_private = (void *)dev_priv; + dev_priv->dev = dev; /* Add register map (needed for suspend/resume) */ base = drm_get_resource_start(dev, mmio_bar); @@ -1052,8 +1053,11 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags) _DRM_KERNEL | _DRM_DRIVER, &dev_priv->mmio_map); 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_WORK(&dev_priv->user_interrupt_task, + i915_user_interrupt_handler); dev_priv->mm.next_gem_seqno = 1; #ifdef __linux__ diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index e3f280d5..029c39a0 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -101,6 +101,8 @@ typedef struct _drm_i915_vbl_swap { } drm_i915_vbl_swap_t; typedef struct drm_i915_private { + struct drm_device *dev; + drm_local_map_t *sarea; drm_local_map_t *mmio_map; @@ -244,6 +246,7 @@ typedef struct drm_i915_private { struct { struct drm_memrange gtt_space; + /** * List of objects currently involved in rendering from the * ringbuffer. @@ -251,10 +254,20 @@ typedef struct drm_i915_private { * A reference is held on the buffer while on this list. */ struct list_head active_list; + /** - * LRU List of non-executing objects still in the GTT. - * There may still be dirty cachelines that need to be flushed - * before unbind. + * List of objects which are not in the ringbuffer but which + * still have a write_domain which needs to be flushed before + * unbinding. + * + * A reference is held on the buffer while on this list. + */ + struct list_head flushing_list; + + /** + * LRU list of objects which are not in the ringbuffer and + * are ready to unbind, but are still in the GTT. + * * A reference is not held on the buffer while on this list, * as merely being GTT-bound shouldn't prevent its being * freed, and we'll pull it off the list in the free path. @@ -269,6 +282,8 @@ typedef struct drm_i915_private { uint32_t next_gem_seqno; } mm; + + struct work_struct user_interrupt_task; } drm_i915_private_t; enum intel_chip_family { @@ -285,11 +300,11 @@ struct drm_i915_gem_object { /** Current space allocated to this object in the GTT, if any. */ struct drm_memrange_node *gtt_space; - /** This object's place on the active or inactive lists */ + /** This object's place on the active/flushing/inactive lists */ struct list_head list; /** - * This is set if the object is on the active list + * This is set if the object is on the active or flushing lists * (has pending rendering), and is not set if it's on inactive (ready * to be unbound). */ @@ -334,6 +349,9 @@ struct drm_i915_gem_request { /** Time at which this request was emitted, in jiffies. */ unsigned long emitted_jiffies; + /** Cache domains that were flushed at the start of the request. */ + uint32_t flush_domains; + struct list_head list; }; @@ -385,6 +403,7 @@ extern int i915_vblank_swap(struct drm_device *dev, void *data, struct drm_file *file_priv); extern void i915_user_irq_on(drm_i915_private_t *dev_priv); extern void i915_user_irq_off(drm_i915_private_t *dev_priv); +extern void i915_user_interrupt_handler(struct work_struct *work); /* i915_mem.c */ extern int i915_mem_alloc(struct drm_device *dev, void *data, @@ -438,6 +457,7 @@ int i915_gem_set_domain(struct drm_gem_object *obj, int i915_gem_flush_pwrite(struct drm_gem_object *obj, uint64_t offset, uint64_t size); void i915_gem_lastclose(struct drm_device *dev); +void i915_gem_retire_requests(struct drm_device *dev); #endif #ifdef __linux__ diff --git a/shared-core/i915_irq.c b/shared-core/i915_irq.c index b17e2408..02191316 100644 --- a/shared-core/i915_irq.c +++ b/shared-core/i915_irq.c @@ -436,6 +436,28 @@ u32 i915_get_vblank_counter(struct drm_device *dev, int plane) return count; } +/** + * Handler for user interrupts in process context (able to sleep, do VFS + * operations, etc. + * + * If another IRQ comes in while we're in this handler, it will still get put + * on the queue again to be rerun when we finish. + */ +void +i915_user_interrupt_handler(struct work_struct *work) +{ + drm_i915_private_t *dev_priv; + struct drm_device *dev; + + dev_priv = container_of(work, drm_i915_private_t, + user_interrupt_task); + dev = dev_priv->dev; + + mutex_lock(&dev->struct_mutex); + i915_gem_retire_requests(dev); + mutex_unlock(&dev->struct_mutex); +} + irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS) { struct drm_device *dev = (struct drm_device *) arg; @@ -493,6 +515,7 @@ irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS) DRM_WAKEUP(&dev_priv->irq_queue); #ifdef I915_HAVE_FENCE i915_fence_handler(dev); + schedule_work(&dev_priv->user_interrupt_task); #endif } -- 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(-) 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 a51c3a76ff415104426493a97ac686ccfe3f3926 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 22 May 2008 10:48:32 -0700 Subject: [intel] Add debug code to verify the cached ring tail pointer. Recording the tail pointer in a local variable improves performance, but if someone messes up and fails to reload at the right time, the driver will write commands to the wrong part of the ring and scramble execution badly. This change (available by setting I915_RING_VALIDATE to 1) checks to make sure the cached tail pointer matches the hardware tail pointer at each ring buffer addition, calling BUG_ON when that's not true. --- shared-core/i915_dma.c | 24 ++++++++++++++++++++++++ shared-core/i915_drv.h | 10 ++++++++++ 2 files changed, 34 insertions(+) diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index fa73cd29..1fc617d1 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -69,6 +69,30 @@ int i915_wait_ring(struct drm_device * dev, int n, const char *caller) return -EBUSY; } +#if I915_RING_VALIDATE +/** + * Validate the cached ring tail value + * + * If the X server writes to the ring and DRM doesn't + * reload the head and tail pointers, it will end up writing + * data to the wrong place in the ring, causing havoc. + */ +void i915_ring_validate(struct drm_device *dev, const char *func, int line) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + drm_i915_ring_buffer_t *ring = &(dev_priv->ring); + u32 tail = I915_READ(LP_RING+RING_TAIL) & HEAD_ADDR; + u32 head = I915_READ(LP_RING+RING_HEAD) & HEAD_ADDR; + + if (tail != ring->tail) { + DRM_ERROR("%s:%d head sw %x, hw %x. tail sw %x hw %x\n", + func, line, + ring->head, head, ring->tail, tail); + BUG_ON(1); + } +} +#endif + void i915_kernel_lost_context(struct drm_device * dev) { drm_i915_private_t *dev_priv = dev->dev_private; diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 029c39a0..e217b789 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -473,14 +473,23 @@ extern void intel_fini_chipset_flush_compat(struct drm_device *dev); #define I915_WRITE16(reg,val) DRM_WRITE16(dev_priv->mmio_map, (reg), (val)) #define I915_VERBOSE 0 +#define I915_RING_VALIDATE 0 #define RING_LOCALS unsigned int outring, ringmask, outcount; \ volatile char *virt; +#if I915_RING_VALIDATE +void i915_ring_validate(struct drm_device *dev, const char *func, int line); +#define I915_RING_DO_VALIDATE(dev) i915_ring_validate(dev, __FUNCTION__, __LINE__) +#else +#define I915_RING_DO_VALIDATE(dev) +#endif + #define BEGIN_LP_RING(n) do { \ if (I915_VERBOSE) \ DRM_DEBUG("BEGIN_LP_RING(%d)\n", \ (n)); \ + I915_RING_DO_VALIDATE(dev); \ if (dev_priv->ring.space < (n)*4) \ i915_wait_ring(dev, (n)*4, __FUNCTION__); \ outcount = 0; \ @@ -499,6 +508,7 @@ extern void intel_fini_chipset_flush_compat(struct drm_device *dev); #define ADVANCE_LP_RING() do { \ if (I915_VERBOSE) DRM_DEBUG("ADVANCE_LP_RING %x\n", outring); \ + I915_RING_DO_VALIDATE(dev); \ dev_priv->ring.tail = outring; \ dev_priv->ring.space -= outcount * 4; \ I915_WRITE(LP_RING + RING_TAIL, outring); \ -- 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(+) 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(-) 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(-) 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(-) 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(-) 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 +++++++++++++++++++++++++ shared-core/i915_dma.c | 1 + shared-core/i915_drm.h | 9 +++++++++ shared-core/i915_drv.h | 2 ++ 4 files changed, 37 insertions(+) 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; diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 1fc617d1..76cd45f1 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1180,6 +1180,7 @@ struct drm_ioctl_desc i915_ioctls[] = { DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH), DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), + DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH), }; int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls); diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index b50471f3..a8e09e72 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -180,6 +180,7 @@ typedef struct drm_i915_sarea { #define DRM_I915_GEM_EXECBUFFER 0x14 #define DRM_I915_GEM_PIN 0x15 #define DRM_I915_GEM_UNPIN 0x16 +#define DRM_I915_GEM_BUSY 0x17 #define DRM_IOCTL_I915_INIT DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t) #define DRM_IOCTL_I915_FLUSH DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH) @@ -203,6 +204,7 @@ typedef struct drm_i915_sarea { #define DRM_IOCTL_I915_GEM_EXECBUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_EXECBUFFER, struct drm_i915_gem_execbuffer) #define DRM_IOCTL_I915_GEM_PIN DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_PIN, struct drm_i915_gem_pin) #define DRM_IOCTL_I915_GEM_UNPIN DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_UNPIN, struct drm_i915_gem_unpin) +#define DRM_IOCTL_I915_GEM_BUSY DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_BUSY, struct drm_i915_gem_busy) /* Asynchronous page flipping: */ @@ -536,5 +538,12 @@ struct drm_i915_gem_unpin { uint32_t pad; }; +struct drm_i915_gem_busy { + /** Handle of the buffer to check for busy */ + uint32_t handle; + + /** Return busy status (1 if busy, 0 if idle) */ + uint32_t busy; +}; #endif /* _I915_DRM_H_ */ diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index e217b789..04e149a9 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -449,6 +449,8 @@ int i915_gem_pin_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int i915_gem_unpin_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); +int i915_gem_busy_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); int i915_gem_init_object(struct drm_gem_object *obj); void i915_gem_free_object(struct drm_gem_object *obj); int i915_gem_set_domain(struct drm_gem_object *obj, -- 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 ++++++---- shared-core/i915_drv.h | 2 +- shared-core/i915_irq.c | 74 ++++++++++++++++++++++++++++++++++---------------- 3 files changed, 60 insertions(+), 30 deletions(-) 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; } diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 04e149a9..fee6d436 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -131,7 +131,7 @@ typedef struct drm_i915_private { DRM_SPINTYPE user_irq_lock; int user_irq_refcount; int fence_irq_on; - uint32_t irq_enable_reg; + uint32_t irq_mask_reg; int irq_enabled; #ifdef I915_HAVE_FENCE diff --git a/shared-core/i915_irq.c b/shared-core/i915_irq.c index 02191316..fbfc5f46 100644 --- a/shared-core/i915_irq.c +++ b/shared-core/i915_irq.c @@ -508,7 +508,7 @@ irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS) if (dev_priv->sarea_priv) dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv); - I915_WRITE(I915REG_INT_IDENTITY_R, iir); + I915_WRITE(I915REG_INT_IDENTITY_R, iir | I915_USER_INTERRUPT); (void) I915_READ(I915REG_INT_IDENTITY_R); /* Flush posted write */ if (iir & I915_USER_INTERRUPT) { @@ -550,8 +550,9 @@ void i915_user_irq_on(drm_i915_private_t *dev_priv) { DRM_SPINLOCK(&dev_priv->user_irq_lock); if (dev_priv->irq_enabled && (++dev_priv->user_irq_refcount == 1)){ - dev_priv->irq_enable_reg |= I915_USER_INTERRUPT; - I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg); + dev_priv->irq_mask_reg &= ~I915_USER_INTERRUPT; + I915_WRITE(I915REG_INT_MASK_R, dev_priv->irq_mask_reg); + (void) I915_READ (I915REG_INT_ENABLE_R); } DRM_SPINUNLOCK(&dev_priv->user_irq_lock); @@ -560,9 +561,11 @@ void i915_user_irq_on(drm_i915_private_t *dev_priv) void i915_user_irq_off(drm_i915_private_t *dev_priv) { DRM_SPINLOCK(&dev_priv->user_irq_lock); + BUG_ON(dev_priv->user_irq_refcount <= 0); if (dev_priv->irq_enabled && (--dev_priv->user_irq_refcount == 0)) { - // dev_priv->irq_enable_reg &= ~USER_INT_FLAG; - // I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg); + dev_priv->irq_mask_reg |= I915_USER_INTERRUPT; + I915_WRITE(I915REG_INT_MASK_R, dev_priv->irq_mask_reg); + (void) I915_READ(I915REG_INT_MASK_R); } DRM_SPINUNLOCK(&dev_priv->user_irq_lock); } @@ -642,16 +645,17 @@ int i915_enable_vblank(struct drm_device *dev, int plane) drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; int pipe = i915_get_pipe(dev, plane); u32 pipestat_reg = 0; + u32 mask_reg = 0; u32 pipestat; switch (pipe) { case 0: pipestat_reg = I915REG_PIPEASTAT; - dev_priv->irq_enable_reg |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT; + mask_reg |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT; break; case 1: pipestat_reg = I915REG_PIPEBSTAT; - dev_priv->irq_enable_reg |= I915_DISPLAY_PIPE_B_EVENT_INTERRUPT; + mask_reg |= I915_DISPLAY_PIPE_B_EVENT_INTERRUPT; break; default: DRM_ERROR("tried to enable vblank on non-existent pipe %d\n", @@ -677,7 +681,11 @@ int i915_enable_vblank(struct drm_device *dev, int plane) I915_VBLANK_INTERRUPT_STATUS); I915_WRITE(pipestat_reg, pipestat); } - I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg); + DRM_SPINLOCK(&dev_priv->user_irq_lock); + dev_priv->irq_mask_reg &= ~mask_reg; + I915_WRITE(I915REG_INT_MASK_R, dev_priv->irq_mask_reg); + I915_READ(I915REG_INT_MASK_R); + DRM_SPINUNLOCK(&dev_priv->user_irq_lock); return 0; } @@ -687,16 +695,17 @@ void i915_disable_vblank(struct drm_device *dev, int plane) drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; int pipe = i915_get_pipe(dev, plane); u32 pipestat_reg = 0; + u32 mask_reg = 0; u32 pipestat; switch (pipe) { case 0: pipestat_reg = I915REG_PIPEASTAT; - dev_priv->irq_enable_reg &= ~I915_DISPLAY_PIPE_A_EVENT_INTERRUPT; + mask_reg |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT; break; case 1: pipestat_reg = I915REG_PIPEBSTAT; - dev_priv->irq_enable_reg &= ~I915_DISPLAY_PIPE_B_EVENT_INTERRUPT; + mask_reg |= I915_DISPLAY_PIPE_B_EVENT_INTERRUPT; break; default: DRM_ERROR("tried to disable vblank on non-existent pipe %d\n", @@ -704,7 +713,11 @@ void i915_disable_vblank(struct drm_device *dev, int plane) break; } - I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg); + DRM_SPINLOCK(&dev_priv->user_irq_lock); + dev_priv->irq_mask_reg |= mask_reg; + I915_WRITE(I915REG_INT_MASK_R, dev_priv->irq_mask_reg); + (void) I915_READ (I915REG_INT_MASK_R); + DRM_SPINUNLOCK(&dev_priv->user_irq_lock); if (pipestat_reg) { pipestat = I915_READ (pipestat_reg); @@ -716,6 +729,7 @@ void i915_disable_vblank(struct drm_device *dev, int plane) pipestat |= (I915_START_VBLANK_INTERRUPT_STATUS | I915_VBLANK_INTERRUPT_STATUS); I915_WRITE(pipestat_reg, pipestat); + (void) I915_READ(pipestat_reg); } } @@ -723,12 +737,27 @@ static void i915_enable_interrupt (struct drm_device *dev) { drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; - dev_priv->irq_enable_reg |= I915_USER_INTERRUPT; - - I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg); + dev_priv->irq_mask_reg = (I915_USER_INTERRUPT | + I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | + I915_DISPLAY_PIPE_B_EVENT_INTERRUPT); + I915_WRITE(I915REG_INT_MASK_R, dev_priv->irq_mask_reg); + I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_mask_reg); + (void) I915_READ (I915REG_INT_ENABLE_R); dev_priv->irq_enabled = 1; } +static void i915_disable_interrupt (struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; + + I915_WRITE(I915REG_HWSTAM, 0xffffffff); + I915_WRITE(I915REG_INT_MASK_R, 0xffffffff); + I915_WRITE(I915REG_INT_ENABLE_R, 0); + I915_WRITE(I915REG_INT_IDENTITY_R, 0xffffffff); + (void) I915_READ (I915REG_INT_IDENTITY_R); + dev_priv->irq_enabled = 0; +} + /* Set the vblank monitor pipe */ int i915_vblank_pipe_set(struct drm_device *dev, void *data, @@ -934,9 +963,11 @@ void i915_driver_irq_preinstall(struct drm_device * dev) { drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; - I915_WRITE16(I915REG_HWSTAM, 0xeffe); - I915_WRITE16(I915REG_INT_MASK_R, 0x0); - I915_WRITE16(I915REG_INT_ENABLE_R, 0x0); + I915_WRITE(I915REG_HWSTAM, 0xffff); + I915_WRITE(I915REG_INT_ENABLE_R, 0x0); + I915_WRITE(I915REG_INT_MASK_R, 0xffffffff); + I915_WRITE(I915REG_INT_IDENTITY_R, 0xffffffff); + (void) I915_READ(I915REG_INT_IDENTITY_R); } int i915_driver_irq_postinstall(struct drm_device * dev) @@ -950,7 +981,7 @@ int i915_driver_irq_postinstall(struct drm_device * dev) DRM_SPININIT(&dev_priv->user_irq_lock, "userirq"); dev_priv->user_irq_refcount = 0; - dev_priv->irq_enable_reg = 0; + dev_priv->irq_mask_reg = 0; ret = drm_vblank_init(dev, num_pipes); if (ret) @@ -977,15 +1008,10 @@ void i915_driver_irq_uninstall(struct drm_device * dev) if (!dev_priv) return; - dev_priv->irq_enabled = 0; - I915_WRITE(I915REG_HWSTAM, 0xffffffff); - I915_WRITE(I915REG_INT_MASK_R, 0xffffffff); - I915_WRITE(I915REG_INT_ENABLE_R, 0x0); + i915_disable_interrupt (dev); temp = I915_READ(I915REG_PIPEASTAT); I915_WRITE(I915REG_PIPEASTAT, temp); temp = I915_READ(I915REG_PIPEBSTAT); I915_WRITE(I915REG_PIPEBSTAT, temp); - temp = I915_READ(I915REG_INT_IDENTITY_R); - I915_WRITE(I915REG_INT_IDENTITY_R, temp); } -- 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(+) 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 ++-- shared-core/i915_drv.h | 1 + 5 files changed, 77 insertions(+), 18 deletions(-) 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; } diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index fee6d436..76bc6bee 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -454,6 +454,7 @@ int i915_gem_busy_ioctl(struct drm_device *dev, void *data, int i915_gem_init_object(struct drm_gem_object *obj); 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); int i915_gem_flush_pwrite(struct drm_gem_object *obj, -- 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(-) 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(+) 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 6e8a2cff66ac0d6afaf9bb233bc81449c2014078 Mon Sep 17 00:00:00 2001 From: Dave Airlie Date: Fri, 30 May 2008 20:27:31 +1000 Subject: r500: attempt to make AGP work by programming agp base in the MC correctly --- shared-core/radeon_cp.c | 26 +++++++++++++++++++++++--- shared-core/radeon_drv.h | 4 ++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/shared-core/radeon_cp.c b/shared-core/radeon_cp.c index 2e680306..819a61ae 100644 --- a/shared-core/radeon_cp.c +++ b/shared-core/radeon_cp.c @@ -112,6 +112,27 @@ static void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_lo RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc); } +static void radeon_write_agp_base(drm_radeon_private_t *dev_priv, u64 agp_base) +{ + u32 agp_base_hi = upper_32_bits(agp_base); + u32 agp_base_lo = agp_base & 0xffffffff; + + if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) { + R500_WRITE_MCIND(RV515_MC_AGP_BASE, agp_base_lo); + R500_WRITE_MCIND(RV515_MC_AGP_BASE_2, agp_base_hi); + } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) { + RS690_WRITE_MCIND(RS690_MC_AGP_BASE, agp_base_lo); + RS690_WRITE_MCIND(RS690_MC_AGP_BASE_2, agp_base_hi); + } else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515) { + R500_WRITE_MCIND(R520_MC_AGP_BASE, agp_base_lo); + R500_WRITE_MCIND(R520_MC_AGP_BASE_2, agp_base_hi); + } else { + RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_base_lo); + if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200) + RADEON_WRITE(RADEON_AGP_BASE_2, agp_base_hi); + } +} + static int RADEON_READ_PLL(struct drm_device * dev, int addr) { drm_radeon_private_t *dev_priv = dev->dev_private; @@ -542,9 +563,8 @@ static void radeon_cp_init_ring_buffer(struct drm_device * dev, #if __OS_HAS_AGP if (dev_priv->flags & RADEON_IS_AGP) { - RADEON_WRITE(RADEON_AGP_BASE, (unsigned int)dev->agp->base); - if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200) - RADEON_WRITE(RADEON_AGP_BASE_2, 0); + radeon_write_agp_base(dev_priv, dev->agp->base); + radeon_write_agp_location(dev_priv, (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) & 0xffff0000) | diff --git a/shared-core/radeon_drv.h b/shared-core/radeon_drv.h index 1b32b2f4..e263c610 100644 --- a/shared-core/radeon_drv.h +++ b/shared-core/radeon_drv.h @@ -524,9 +524,13 @@ extern int r300_do_cp_cmdbuf(struct drm_device *dev, #define RV515_MC_FB_LOCATION 0x01 #define RV515_MC_AGP_LOCATION 0x02 +#define RV515_MC_AGP_BASE 0x03 +#define RV515_MC_AGP_BASE_2 0x04 #define R520_MC_FB_LOCATION 0x04 #define R520_MC_AGP_LOCATION 0x05 +#define R520_MC_AGP_BASE 0x06 +#define R520_MC_AGP_BASE_2 0x07 #define RADEON_MPP_TB_CONFIG 0x01c0 #define RADEON_MEM_CNTL 0x0140 -- 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(+) 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(-) 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(-) 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(-) 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 6a9eb08a872ac0388aad2c901888888964f14559 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 3 Jun 2008 09:27:37 -0700 Subject: Import bufmgr code to libdrm. Not yet hooked up to the build. --- libdrm/dri_bufmgr.c | 171 ++++++ libdrm/dri_bufmgr.h | 216 +++++++ libdrm/intel/intel_bufmgr_fake.c | 1177 ++++++++++++++++++++++++++++++++++++++ libdrm/intel/intel_bufmgr_fake.h | 50 ++ libdrm/intel/intel_bufmgr_gem.c | 847 +++++++++++++++++++++++++++ libdrm/intel/intel_bufmgr_gem.h | 16 + 6 files changed, 2477 insertions(+) create mode 100644 libdrm/dri_bufmgr.c create mode 100644 libdrm/dri_bufmgr.h create mode 100644 libdrm/intel/intel_bufmgr_fake.c create mode 100644 libdrm/intel/intel_bufmgr_fake.h create mode 100644 libdrm/intel/intel_bufmgr_gem.c create mode 100644 libdrm/intel/intel_bufmgr_gem.h diff --git a/libdrm/dri_bufmgr.c b/libdrm/dri_bufmgr.c new file mode 100644 index 00000000..be2a7b74 --- /dev/null +++ b/libdrm/dri_bufmgr.c @@ -0,0 +1,171 @@ +/* + * Copyright © 2007 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 "mtypes.h" +#include "dri_bufmgr.h" + +/** @file dri_bufmgr.c + * + * Convenience functions for buffer management methods. + */ + +dri_bo * +dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size, + unsigned int alignment, uint64_t location_mask) +{ + assert((location_mask & ~(DRM_BO_FLAG_MEM_LOCAL | DRM_BO_FLAG_MEM_TT | + DRM_BO_FLAG_MEM_VRAM | DRM_BO_FLAG_MEM_PRIV0 | + DRM_BO_FLAG_MEM_PRIV1 | DRM_BO_FLAG_MEM_PRIV2 | + DRM_BO_FLAG_MEM_PRIV3 | DRM_BO_FLAG_MEM_PRIV4 | + DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED)) == 0); + return bufmgr->bo_alloc(bufmgr, name, size, alignment, location_mask); +} + +dri_bo * +dri_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, unsigned long offset, + unsigned long size, void *virtual, + uint64_t location_mask) +{ + assert((location_mask & ~(DRM_BO_FLAG_MEM_LOCAL | DRM_BO_FLAG_MEM_TT | + DRM_BO_FLAG_MEM_VRAM | DRM_BO_FLAG_MEM_PRIV0 | + DRM_BO_FLAG_MEM_PRIV1 | DRM_BO_FLAG_MEM_PRIV2 | + DRM_BO_FLAG_MEM_PRIV3 | + DRM_BO_FLAG_MEM_PRIV4)) == 0); + + return bufmgr->bo_alloc_static(bufmgr, name, offset, size, virtual, + location_mask); +} + +void +dri_bo_reference(dri_bo *bo) +{ + bo->bufmgr->bo_reference(bo); +} + +void +dri_bo_unreference(dri_bo *bo) +{ + if (bo == NULL) + return; + + bo->bufmgr->bo_unreference(bo); +} + +int +dri_bo_map(dri_bo *buf, GLboolean write_enable) +{ + return buf->bufmgr->bo_map(buf, write_enable); +} + +int +dri_bo_unmap(dri_bo *buf) +{ + return buf->bufmgr->bo_unmap(buf); +} + +int +dri_bo_subdata(dri_bo *bo, unsigned long offset, + unsigned long size, const void *data) +{ + int ret; + if (bo->bufmgr->bo_subdata) + return bo->bufmgr->bo_subdata(bo, offset, size, data); + if (size == 0 || data == NULL) + return 0; + + ret = dri_bo_map(bo, GL_TRUE); + if (ret) + return ret; + memcpy((unsigned char *)bo->virtual + offset, data, size); + dri_bo_unmap(bo); + return 0; +} + +int +dri_bo_get_subdata(dri_bo *bo, unsigned long offset, + unsigned long size, void *data) +{ + int ret; + if (bo->bufmgr->bo_subdata) + return bo->bufmgr->bo_get_subdata(bo, offset, size, data); + + if (size == 0 || data == NULL) + return 0; + + ret = dri_bo_map(bo, GL_FALSE); + if (ret) + return ret; + memcpy(data, (unsigned char *)bo->virtual + offset, size); + dri_bo_unmap(bo); + return 0; +} + +void +dri_bo_wait_rendering(dri_bo *bo) +{ + bo->bufmgr->bo_wait_rendering(bo); +} + +void +dri_bufmgr_destroy(dri_bufmgr *bufmgr) +{ + bufmgr->destroy(bufmgr); +} + + +int dri_emit_reloc(dri_bo *reloc_buf, + uint32_t read_domains, uint32_t write_domain, + uint32_t delta, uint32_t offset, dri_bo *target_buf) +{ + return reloc_buf->bufmgr->emit_reloc(reloc_buf, read_domains, write_domain, + delta, offset, target_buf); +} + +void *dri_process_relocs(dri_bo *batch_buf) +{ + return batch_buf->bufmgr->process_relocs(batch_buf); +} + +void dri_post_submit(dri_bo *batch_buf) +{ + batch_buf->bufmgr->post_submit(batch_buf); +} + +void +dri_bufmgr_set_debug(dri_bufmgr *bufmgr, GLboolean enable_debug) +{ + bufmgr->debug = enable_debug; +} + +int +dri_bufmgr_check_aperture_space(dri_bo *bo) +{ + return bo->bufmgr->check_aperture_space(bo); +} diff --git a/libdrm/dri_bufmgr.h b/libdrm/dri_bufmgr.h new file mode 100644 index 00000000..1abca08c --- /dev/null +++ b/libdrm/dri_bufmgr.h @@ -0,0 +1,216 @@ +/************************************************************************** + * + * Copyright © 2007 Intel Corporation + * 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 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. + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * + **************************************************************************/ +/* + * Authors: Thomas Hellström + * Keith Whitwell + * Eric Anholt + */ + +#ifndef _DRI_BUFMGR_H_ +#define _DRI_BUFMGR_H_ +#include + +typedef struct _dri_bufmgr dri_bufmgr; +typedef struct _dri_bo dri_bo; + +struct _dri_bo { + /** + * Size in bytes of the buffer object. + * + * The size may be larger than the size originally requested for the + * allocation, such as being aligned to page size. + */ + unsigned long size; + /** + * Card virtual address (offset from the beginning of the aperture) for the + * object. Only valid while validated. + */ + unsigned long offset; + /** + * Virtual address for accessing the buffer data. Only valid while mapped. + */ + void *virtual; + /** Buffer manager context associated with this buffer object */ + dri_bufmgr *bufmgr; +}; + +/** + * Context for a buffer manager instance. + * + * Contains public methods followed by private storage for the buffer manager. + */ +struct _dri_bufmgr { + /** + * Allocate a buffer object. + * + * Buffer objects are not necessarily initially mapped into CPU virtual + * address space or graphics device aperture. They must be mapped using + * bo_map() to be used by the CPU, and validated for use using bo_validate() + * to be used from the graphics device. + */ + dri_bo *(*bo_alloc)(dri_bufmgr *bufmgr_ctx, const char *name, + unsigned long size, unsigned int alignment, + uint64_t location_mask); + + /** + * Allocates a buffer object for a static allocation. + * + * Static allocations are ones such as the front buffer that are offered by + * the X Server, which are never evicted and never moved. + */ + dri_bo *(*bo_alloc_static)(dri_bufmgr *bufmgr_ctx, const char *name, + unsigned long offset, unsigned long size, + void *virtual, uint64_t location_mask); + + /** Takes a reference on a buffer object */ + void (*bo_reference)(dri_bo *bo); + + /** + * Releases a reference on a buffer object, freeing the data if + * rerefences remain. + */ + void (*bo_unreference)(dri_bo *bo); + + /** + * Maps the buffer into userspace. + * + * This function will block waiting for any existing execution on the + * buffer to complete, first. The resulting mapping is available at + * buf->virtual. + */ + int (*bo_map)(dri_bo *buf, GLboolean write_enable); + + /** Reduces the refcount on the userspace mapping of the buffer object. */ + int (*bo_unmap)(dri_bo *buf); + + /** + * Write data into an object. + * + * This is an optional function, if missing, + * dri_bo will map/memcpy/unmap. + */ + int (*bo_subdata) (dri_bo *buf, unsigned long offset, + unsigned long size, const void *data); + + /** + * Read data from an object + * + * This is an optional function, if missing, + * dri_bo will map/memcpy/unmap. + */ + int (*bo_get_subdata) (dri_bo *bo, unsigned long offset, + unsigned long size, void *data); + + /** + * Waits for rendering to an object by the GPU to have completed. + * + * This is not required for any access to the BO by bo_map, bo_subdata, etc. + * It is merely a way for the driver to implement glFinish. + */ + void (*bo_wait_rendering) (dri_bo *bo); + + /** + * Tears down the buffer manager instance. + */ + void (*destroy)(dri_bufmgr *bufmgr); + + /** + * Add relocation entry in reloc_buf, which will be updated with the + * target buffer's real offset on on command submission. + * + * Relocations remain in place for the lifetime of the buffer object. + * + * \param reloc_buf Buffer to write the relocation into. + * \param flags BO flags to be used in validating the target buffer. + * Applicable flags include: + * - DRM_BO_FLAG_READ: The buffer will be read in the process of + * command execution. + * - DRM_BO_FLAG_WRITE: The buffer will be written in the process of + * command execution. + * - DRM_BO_FLAG_MEM_TT: The buffer should be validated in TT memory. + * - DRM_BO_FLAG_MEM_VRAM: The buffer should be validated in video + * memory. + * \param delta Constant value to be added to the relocation target's offset. + * \param offset Byte offset within batch_buf of the relocated pointer. + * \param target Buffer whose offset should be written into the relocation + * entry. + */ + int (*emit_reloc)(dri_bo *reloc_buf, + uint32_t read_domains, uint32_t write_domain, + uint32_t delta, uint32_t offset, dri_bo *target); + + /** + * Processes the relocations, either in userland or by converting the list + * for use in batchbuffer submission. + * + * Kernel-based implementations will return a pointer to the arguments + * to be handed with batchbuffer submission to the kernel. The userland + * implementation performs the buffer validation and emits relocations + * into them the appopriate order. + * + * \param batch_buf buffer at the root of the tree of relocations + * \return argument to be completed and passed to the execbuffers ioctl + * (if any). + */ + void *(*process_relocs)(dri_bo *batch_buf); + + void (*post_submit)(dri_bo *batch_buf); + + int (*check_aperture_space)(dri_bo *bo); + GLboolean debug; /**< Enables verbose debugging printouts */ +}; + +dri_bo *dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size, + unsigned int alignment, uint64_t location_mask); +dri_bo *dri_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, + unsigned long offset, unsigned long size, + void *virtual, uint64_t location_mask); +void dri_bo_reference(dri_bo *bo); +void dri_bo_unreference(dri_bo *bo); +int dri_bo_map(dri_bo *buf, GLboolean write_enable); +int dri_bo_unmap(dri_bo *buf); + +int dri_bo_subdata(dri_bo *bo, unsigned long offset, + unsigned long size, const void *data); +int dri_bo_get_subdata(dri_bo *bo, unsigned long offset, + unsigned long size, void *data); +void dri_bo_wait_rendering(dri_bo *bo); + +void dri_bufmgr_set_debug(dri_bufmgr *bufmgr, GLboolean enable_debug); +void dri_bufmgr_destroy(dri_bufmgr *bufmgr); + +int dri_emit_reloc(dri_bo *reloc_buf, + uint32_t read_domains, uint32_t write_domain, + uint32_t delta, uint32_t offset, dri_bo *target_buf); +void *dri_process_relocs(dri_bo *batch_buf); +void dri_post_process_relocs(dri_bo *batch_buf); +void dri_post_submit(dri_bo *batch_buf); +int dri_bufmgr_check_aperture_space(dri_bo *bo); + +#endif diff --git a/libdrm/intel/intel_bufmgr_fake.c b/libdrm/intel/intel_bufmgr_fake.c new file mode 100644 index 00000000..2aed3d85 --- /dev/null +++ b/libdrm/intel/intel_bufmgr_fake.c @@ -0,0 +1,1177 @@ +/************************************************************************** + * + * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas. + * 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 TUNGSTEN GRAPHICS 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. + * + **************************************************************************/ + +/* Originally a fake version of the buffer manager so that we can + * prototype the changes in a driver fairly quickly, has been fleshed + * out to a fully functional interim solution. + * + * Basically wraps the old style memory management in the new + * programming interface, but is more expressive and avoids many of + * the bugs in the old texture manager. + */ +#include "mtypes.h" +#include "dri_bufmgr.h" +#include "intel_bufmgr_fake.h" +#include "drm.h" +#include "i915_drm.h" + +#include "simple_list.h" +#include "mm.h" +#include "imports.h" + +#define DBG(...) do { \ + if (bufmgr_fake->bufmgr.debug) \ + _mesa_printf(__VA_ARGS__); \ +} while (0) + +/* Internal flags: + */ +#define BM_NO_BACKING_STORE 0x00000001 +#define BM_NO_FENCE_SUBDATA 0x00000002 +#define BM_PINNED 0x00000004 + +/* Wrapper around mm.c's mem_block, which understands that you must + * wait for fences to expire before memory can be freed. This is + * specific to our use of memcpy for uploads - an upload that was + * processed through the command queue wouldn't need to care about + * fences. + */ +#define MAX_RELOCS 4096 + +struct fake_buffer_reloc +{ + /** Buffer object that the relocation points at. */ + dri_bo *target_buf; + /** Offset of the relocation entry within reloc_buf. */ + GLuint offset; + /** Cached value of the offset when we last performed this relocation. */ + GLuint last_target_offset; + /** Value added to target_buf's offset to get the relocation entry. */ + GLuint delta; + /** Cache domains the target buffer is read into. */ + uint32_t read_domains; + /** Cache domain the target buffer will have dirty cachelines in. */ + uint32_t write_domain; +}; + +struct block { + struct block *next, *prev; + struct mem_block *mem; /* BM_MEM_AGP */ + + /** + * Marks that the block is currently in the aperture and has yet to be + * fenced. + */ + unsigned on_hardware:1; + /** + * Marks that the block is currently fenced (being used by rendering) and + * can't be freed until @fence is passed. + */ + unsigned fenced:1; + + /** Fence cookie for the block. */ + unsigned fence; /* Split to read_fence, write_fence */ + + dri_bo *bo; + void *virtual; +}; + +typedef struct _bufmgr_fake { + dri_bufmgr bufmgr; + + unsigned long low_offset; + unsigned long size; + void *virtual; + + struct mem_block *heap; + struct block lru; /* only allocated, non-fence-pending blocks here */ + + unsigned buf_nr; /* for generating ids */ + + struct block on_hardware; /* after bmValidateBuffers */ + struct block fenced; /* after bmFenceBuffers (mi_flush, emit irq, write dword) */ + /* then to bufmgr->lru or free() */ + + unsigned int last_fence; + + unsigned fail:1; + unsigned need_fence:1; + GLboolean thrashing; + + /** + * Driver callback to emit a fence, returning the cookie. + * + * Currently, this also requires that a write flush be emitted before + * emitting the fence, but this should change. + */ + unsigned int (*fence_emit)(void *private); + /** Driver callback to wait for a fence cookie to have passed. */ + int (*fence_wait)(void *private, unsigned int fence_cookie); + /** Driver-supplied argument to driver callbacks */ + void *driver_priv; + + GLboolean debug; + + GLboolean performed_rendering; + + /* keep track of the current total size of objects we have relocs for */ + unsigned long current_total_size; +} dri_bufmgr_fake; + +typedef struct _dri_bo_fake { + dri_bo bo; + + unsigned id; /* debug only */ + const char *name; + + unsigned dirty:1; + unsigned size_accounted:1; /*this buffers size has been accounted against the aperture */ + unsigned card_dirty:1; /* has the card written to this buffer - we make need to copy it back */ + unsigned int refcount; + /* Flags may consist of any of the DRM_BO flags, plus + * DRM_BO_NO_BACKING_STORE and BM_NO_FENCE_SUBDATA, which are the first two + * driver private flags. + */ + uint64_t flags; + /** Cache domains the target buffer is read into. */ + uint32_t read_domains; + /** Cache domain the target buffer will have dirty cachelines in. */ + uint32_t write_domain; + + unsigned int alignment; + GLboolean is_static, validated; + unsigned int map_count; + + /** relocation list */ + struct fake_buffer_reloc *relocs; + GLuint nr_relocs; + + struct block *block; + void *backing_store; + void (*invalidate_cb)(dri_bo *bo, void *ptr); + void *invalidate_ptr; +} dri_bo_fake; + +static int clear_fenced(dri_bufmgr_fake *bufmgr_fake, + unsigned int fence_cookie); + +static int dri_fake_check_aperture_space(dri_bo *bo); + +#define MAXFENCE 0x7fffffff + +static GLboolean FENCE_LTE( unsigned a, unsigned b ) +{ + if (a == b) + return GL_TRUE; + + if (a < b && b - a < (1<<24)) + return GL_TRUE; + + if (a > b && MAXFENCE - a + b < (1<<24)) + return GL_TRUE; + + return GL_FALSE; +} + +static unsigned int +_fence_emit_internal(dri_bufmgr_fake *bufmgr_fake) +{ + bufmgr_fake->last_fence = bufmgr_fake->fence_emit(bufmgr_fake->driver_priv); + return bufmgr_fake->last_fence; +} + +static void +_fence_wait_internal(dri_bufmgr_fake *bufmgr_fake, unsigned int cookie) +{ + int ret; + + ret = bufmgr_fake->fence_wait(bufmgr_fake->driver_priv, cookie); + if (ret != 0) { + _mesa_printf("%s:%d: Error %d waiting for fence.\n", + __FILE__, __LINE__); + abort(); + } + clear_fenced(bufmgr_fake, cookie); +} + +static GLboolean +_fence_test(dri_bufmgr_fake *bufmgr_fake, unsigned fence) +{ + /* Slight problem with wrap-around: + */ + return fence == 0 || FENCE_LTE(fence, bufmgr_fake->last_fence); +} + +/** + * Allocate a memory manager block for the buffer. + */ +static GLboolean +alloc_block(dri_bo *bo) +{ + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + dri_bufmgr_fake *bufmgr_fake= (dri_bufmgr_fake *)bo->bufmgr; + struct block *block = (struct block *)calloc(sizeof *block, 1); + unsigned int align_log2 = _mesa_ffs(bo_fake->alignment) - 1; + GLuint sz; + + if (!block) + return GL_FALSE; + + sz = (bo->size + bo_fake->alignment - 1) & ~(bo_fake->alignment - 1); + + block->mem = mmAllocMem(bufmgr_fake->heap, sz, align_log2, 0); + if (!block->mem) { + free(block); + return GL_FALSE; + } + + make_empty_list(block); + + /* Insert at head or at tail??? + */ + insert_at_tail(&bufmgr_fake->lru, block); + + block->virtual = bufmgr_fake->virtual + + block->mem->ofs - bufmgr_fake->low_offset; + block->bo = bo; + + bo_fake->block = block; + + return GL_TRUE; +} + +/* Release the card storage associated with buf: + */ +static void free_block(dri_bufmgr_fake *bufmgr_fake, struct block *block) +{ + dri_bo_fake *bo_fake; + DBG("free block %p %08x %d %d\n", block, block->mem->ofs, block->on_hardware, block->fenced); + + if (!block) + return; + + bo_fake = (dri_bo_fake *)block->bo; + if (!(bo_fake->flags & BM_NO_BACKING_STORE) && (bo_fake->card_dirty == 1)) { + memcpy(bo_fake->backing_store, block->virtual, block->bo->size); + bo_fake->card_dirty = 1; + bo_fake->dirty = 1; + } + + if (block->on_hardware) { + block->bo = NULL; + } + else if (block->fenced) { + block->bo = NULL; + } + else { + DBG(" - free immediately\n"); + remove_from_list(block); + + mmFreeMem(block->mem); + free(block); + } +} + +static void +alloc_backing_store(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + assert(!bo_fake->backing_store); + assert(!(bo_fake->flags & (BM_PINNED|BM_NO_BACKING_STORE))); + + bo_fake->backing_store = ALIGN_MALLOC(bo->size, 64); + + DBG("alloc_backing - buf %d %p %d\n", bo_fake->id, bo_fake->backing_store, bo->size); + assert(bo_fake->backing_store); +} + +static void +free_backing_store(dri_bo *bo) +{ + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + if (bo_fake->backing_store) { + assert(!(bo_fake->flags & (BM_PINNED|BM_NO_BACKING_STORE))); + ALIGN_FREE(bo_fake->backing_store); + bo_fake->backing_store = NULL; + } +} + +static void +set_dirty(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + if (bo_fake->flags & BM_NO_BACKING_STORE && bo_fake->invalidate_cb != NULL) + bo_fake->invalidate_cb(bo, bo_fake->invalidate_ptr); + + assert(!(bo_fake->flags & BM_PINNED)); + + DBG("set_dirty - buf %d\n", bo_fake->id); + bo_fake->dirty = 1; +} + +static GLboolean +evict_lru(dri_bufmgr_fake *bufmgr_fake, GLuint max_fence) +{ + struct block *block, *tmp; + + DBG("%s\n", __FUNCTION__); + + foreach_s(block, tmp, &bufmgr_fake->lru) { + dri_bo_fake *bo_fake = (dri_bo_fake *)block->bo; + + if (bo_fake != NULL && (bo_fake->flags & BM_NO_FENCE_SUBDATA)) + continue; + + if (block->fence && max_fence && !FENCE_LTE(block->fence, max_fence)) + return 0; + + set_dirty(&bo_fake->bo); + bo_fake->block = NULL; + + free_block(bufmgr_fake, block); + return GL_TRUE; + } + + return GL_FALSE; +} + +#define foreach_s_rev(ptr, t, list) \ + for(ptr=(list)->prev,t=(ptr)->prev; list != ptr; ptr=t, t=(t)->prev) + +static GLboolean +evict_mru(dri_bufmgr_fake *bufmgr_fake) +{ + struct block *block, *tmp; + + DBG("%s\n", __FUNCTION__); + + foreach_s_rev(block, tmp, &bufmgr_fake->lru) { + dri_bo_fake *bo_fake = (dri_bo_fake *)block->bo; + + if (bo_fake && (bo_fake->flags & BM_NO_FENCE_SUBDATA)) + continue; + + set_dirty(&bo_fake->bo); + bo_fake->block = NULL; + + free_block(bufmgr_fake, block); + return GL_TRUE; + } + + return GL_FALSE; +} + +/** + * Removes all objects from the fenced list older than the given fence. + */ +static int clear_fenced(dri_bufmgr_fake *bufmgr_fake, + unsigned int fence_cookie) +{ + struct block *block, *tmp; + int ret = 0; + + foreach_s(block, tmp, &bufmgr_fake->fenced) { + assert(block->fenced); + + if (_fence_test(bufmgr_fake, block->fence)) { + + block->fenced = 0; + + if (!block->bo) { + DBG("delayed free: offset %x sz %x\n", + block->mem->ofs, block->mem->size); + remove_from_list(block); + mmFreeMem(block->mem); + free(block); + } + else { + DBG("return to lru: offset %x sz %x\n", + block->mem->ofs, block->mem->size); + move_to_tail(&bufmgr_fake->lru, block); + } + + ret = 1; + } + else { + /* Blocks are ordered by fence, so if one fails, all from + * here will fail also: + */ + DBG("fence not passed: offset %x sz %x %d %d \n", + block->mem->ofs, block->mem->size, block->fence, bufmgr_fake->last_fence); + break; + } + } + + DBG("%s: %d\n", __FUNCTION__, ret); + return ret; +} + +static void fence_blocks(dri_bufmgr_fake *bufmgr_fake, unsigned fence) +{ + struct block *block, *tmp; + + foreach_s (block, tmp, &bufmgr_fake->on_hardware) { + DBG("Fence block %p (sz 0x%x ofs %x buf %p) with fence %d\n", block, + block->mem->size, block->mem->ofs, block->bo, fence); + block->fence = fence; + + block->on_hardware = 0; + block->fenced = 1; + + /* Move to tail of pending list here + */ + move_to_tail(&bufmgr_fake->fenced, block); + } + + assert(is_empty_list(&bufmgr_fake->on_hardware)); +} + +static GLboolean evict_and_alloc_block(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + assert(bo_fake->block == NULL); + + /* Search for already free memory: + */ + if (alloc_block(bo)) + return GL_TRUE; + + /* If we're not thrashing, allow lru eviction to dig deeper into + * recently used textures. We'll probably be thrashing soon: + */ + if (!bufmgr_fake->thrashing) { + while (evict_lru(bufmgr_fake, 0)) + if (alloc_block(bo)) + return GL_TRUE; + } + + /* Keep thrashing counter alive? + */ + if (bufmgr_fake->thrashing) + bufmgr_fake->thrashing = 20; + + /* Wait on any already pending fences - here we are waiting for any + * freed memory that has been submitted to hardware and fenced to + * become available: + */ + while (!is_empty_list(&bufmgr_fake->fenced)) { + GLuint fence = bufmgr_fake->fenced.next->fence; + _fence_wait_internal(bufmgr_fake, fence); + + if (alloc_block(bo)) + return GL_TRUE; + } + + if (!is_empty_list(&bufmgr_fake->on_hardware)) { + while (!is_empty_list(&bufmgr_fake->fenced)) { + GLuint fence = bufmgr_fake->fenced.next->fence; + _fence_wait_internal(bufmgr_fake, fence); + } + + if (!bufmgr_fake->thrashing) { + DBG("thrashing\n"); + } + bufmgr_fake->thrashing = 20; + + if (alloc_block(bo)) + return GL_TRUE; + } + + while (evict_mru(bufmgr_fake)) + if (alloc_block(bo)) + return GL_TRUE; + + DBG("%s 0x%x bytes failed\n", __FUNCTION__, bo->size); + + return GL_FALSE; +} + +/*********************************************************************** + * Public functions + */ + +/** + * Wait for hardware idle by emitting a fence and waiting for it. + */ +static void +dri_bufmgr_fake_wait_idle(dri_bufmgr_fake *bufmgr_fake) +{ + unsigned int cookie; + + cookie = bufmgr_fake->fence_emit(bufmgr_fake->driver_priv); + _fence_wait_internal(bufmgr_fake, cookie); +} + +/** + * Wait for rendering to a buffer to complete. + * + * It is assumed that the bathcbuffer which performed the rendering included + * the necessary flushing. + */ +static void +dri_fake_bo_wait_rendering(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + if (bo_fake->block == NULL || !bo_fake->block->fenced) + return; + + _fence_wait_internal(bufmgr_fake, bo_fake->block->fence); +} + +/* Specifically ignore texture memory sharing. + * -- just evict everything + * -- and wait for idle + */ +void +dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bufmgr; + struct block *block, *tmp; + + bufmgr_fake->need_fence = 1; + bufmgr_fake->fail = 0; + + /* Wait for hardware idle. We don't know where acceleration has been + * happening, so we'll need to wait anyway before letting anything get + * put on the card again. + */ + dri_bufmgr_fake_wait_idle(bufmgr_fake); + + /* Check that we hadn't released the lock without having fenced the last + * set of buffers. + */ + assert(is_empty_list(&bufmgr_fake->fenced)); + assert(is_empty_list(&bufmgr_fake->on_hardware)); + + foreach_s(block, tmp, &bufmgr_fake->lru) { + assert(_fence_test(bufmgr_fake, block->fence)); + set_dirty(block->bo); + } +} + +static dri_bo * +dri_fake_bo_alloc(dri_bufmgr *bufmgr, const char *name, + unsigned long size, unsigned int alignment, + uint64_t location_mask) +{ + dri_bufmgr_fake *bufmgr_fake; + dri_bo_fake *bo_fake; + + bufmgr_fake = (dri_bufmgr_fake *)bufmgr; + + assert(size != 0); + + bo_fake = calloc(1, sizeof(*bo_fake)); + if (!bo_fake) + return NULL; + + bo_fake->bo.size = size; + bo_fake->bo.offset = -1; + bo_fake->bo.virtual = NULL; + bo_fake->bo.bufmgr = bufmgr; + bo_fake->refcount = 1; + + /* Alignment must be a power of two */ + assert((alignment & (alignment - 1)) == 0); + if (alignment == 0) + alignment = 1; + bo_fake->alignment = alignment; + bo_fake->id = ++bufmgr_fake->buf_nr; + bo_fake->name = name; + bo_fake->flags = 0; + bo_fake->is_static = GL_FALSE; + + DBG("drm_bo_alloc: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name, + bo_fake->bo.size / 1024); + + return &bo_fake->bo; +} + +static dri_bo * +dri_fake_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, + unsigned long offset, unsigned long size, + void *virtual, uint64_t location_mask) +{ + dri_bufmgr_fake *bufmgr_fake; + dri_bo_fake *bo_fake; + + bufmgr_fake = (dri_bufmgr_fake *)bufmgr; + + assert(size != 0); + + bo_fake = calloc(1, sizeof(*bo_fake)); + if (!bo_fake) + return NULL; + + bo_fake->bo.size = size; + bo_fake->bo.offset = offset; + bo_fake->bo.virtual = virtual; + bo_fake->bo.bufmgr = bufmgr; + bo_fake->refcount = 1; + bo_fake->id = ++bufmgr_fake->buf_nr; + bo_fake->name = name; + bo_fake->flags = BM_PINNED | DRM_BO_FLAG_NO_MOVE; + bo_fake->is_static = GL_TRUE; + + DBG("drm_bo_alloc_static: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name, + bo_fake->bo.size / 1024); + + return &bo_fake->bo; +} + +static void +dri_fake_bo_reference(dri_bo *bo) +{ + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + bo_fake->refcount++; +} + +static void +dri_fake_bo_unreference(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + int i; + + if (!bo) + return; + + if (--bo_fake->refcount == 0) { + assert(bo_fake->map_count == 0); + /* No remaining references, so free it */ + if (bo_fake->block) + free_block(bufmgr_fake, bo_fake->block); + free_backing_store(bo); + + for (i = 0; i < bo_fake->nr_relocs; i++) + dri_bo_unreference(bo_fake->relocs[i].target_buf); + + DBG("drm_bo_unreference: free buf %d %s\n", bo_fake->id, bo_fake->name); + + free(bo_fake->relocs); + free(bo); + + return; + } +} + +/** + * Set the buffer as not requiring backing store, and instead get the callback + * invoked whenever it would be set dirty. + */ +void dri_bo_fake_disable_backing_store(dri_bo *bo, + void (*invalidate_cb)(dri_bo *bo, + void *ptr), + void *ptr) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + if (bo_fake->backing_store) + free_backing_store(bo); + + bo_fake->flags |= BM_NO_BACKING_STORE; + + DBG("disable_backing_store set buf %d dirty\n", bo_fake->id); + bo_fake->dirty = 1; + bo_fake->invalidate_cb = invalidate_cb; + bo_fake->invalidate_ptr = ptr; + + /* Note that it is invalid right from the start. Also note + * invalidate_cb is called with the bufmgr locked, so cannot + * itself make bufmgr calls. + */ + if (invalidate_cb != NULL) + invalidate_cb(bo, ptr); +} + +/** + * Map a buffer into bo->virtual, allocating either card memory space (If + * BM_NO_BACKING_STORE or BM_PINNED) or backing store, as necessary. + */ +static int +dri_fake_bo_map(dri_bo *bo, GLboolean write_enable) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + /* Static buffers are always mapped. */ + if (bo_fake->is_static) + return 0; + + /* Allow recursive mapping. Mesa may recursively map buffers with + * nested display loops, and it is used internally in bufmgr_fake + * for relocation. + */ + if (bo_fake->map_count++ != 0) + return 0; + + { + DBG("drm_bo_map: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name, + bo_fake->bo.size / 1024); + + if (bo->virtual != NULL) { + _mesa_printf("%s: already mapped\n", __FUNCTION__); + abort(); + } + else if (bo_fake->flags & (BM_NO_BACKING_STORE|BM_PINNED)) { + + if (!bo_fake->block && !evict_and_alloc_block(bo)) { + DBG("%s: alloc failed\n", __FUNCTION__); + bufmgr_fake->fail = 1; + return 1; + } + else { + assert(bo_fake->block); + bo_fake->dirty = 0; + + if (!(bo_fake->flags & BM_NO_FENCE_SUBDATA) && + bo_fake->block->fenced) { + dri_fake_bo_wait_rendering(bo); + } + + bo->virtual = bo_fake->block->virtual; + } + } + else { + if (write_enable) + set_dirty(bo); + + if (bo_fake->backing_store == 0) + alloc_backing_store(bo); + + bo->virtual = bo_fake->backing_store; + } + } + + return 0; +} + +static int +dri_fake_bo_unmap(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + /* Static buffers are always mapped. */ + if (bo_fake->is_static) + return 0; + + assert(bo_fake->map_count != 0); + if (--bo_fake->map_count != 0) + return 0; + + DBG("drm_bo_unmap: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name, + bo_fake->bo.size / 1024); + + bo->virtual = NULL; + + return 0; +} + +static void +dri_fake_kick_all(dri_bufmgr_fake *bufmgr_fake) +{ + struct block *block, *tmp; + + bufmgr_fake->performed_rendering = GL_FALSE; + /* okay for ever BO that is on the HW kick it off. + seriously not afraid of the POLICE right now */ + foreach_s(block, tmp, &bufmgr_fake->on_hardware) { + dri_bo_fake *bo_fake = (dri_bo_fake *)block->bo; + + block->on_hardware = 0; + free_block(bufmgr_fake, block); + bo_fake->block = NULL; + bo_fake->validated = GL_FALSE; + if (!(bo_fake->flags & BM_NO_BACKING_STORE)) + bo_fake->dirty = 1; + } +} + +static int +dri_fake_bo_validate(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + + /* XXX: Sanity-check whether we've already validated this one under + * different flags. See drmAddValidateItem(). + */ + bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + + DBG("drm_bo_validate: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name, + bo_fake->bo.size / 1024); + + /* Sanity check: Buffers should be unmapped before being validated. + * This is not so much of a problem for bufmgr_fake, but TTM refuses, + * and the problem is harder to debug there. + */ + assert(bo_fake->map_count == 0); + + if (bo_fake->is_static) { + /* Add it to the needs-fence list */ + bufmgr_fake->need_fence = 1; + return 0; + } + + /* reset size accounted */ + bo_fake->size_accounted = 0; + + /* Allocate the card memory */ + if (!bo_fake->block && !evict_and_alloc_block(bo)) { + bufmgr_fake->fail = 1; + DBG("Failed to validate buf %d:%s\n", bo_fake->id, bo_fake->name); + return -1; + } + + assert(bo_fake->block); + assert(bo_fake->block->bo == &bo_fake->bo); + + bo->offset = bo_fake->block->mem->ofs; + + /* Upload the buffer contents if necessary */ + if (bo_fake->dirty) { + DBG("Upload dirty buf %d:%s, sz %d offset 0x%x\n", bo_fake->id, + bo_fake->name, bo->size, bo_fake->block->mem->ofs); + + assert(!(bo_fake->flags & + (BM_NO_BACKING_STORE|BM_PINNED))); + + /* Actually, should be able to just wait for a fence on the memory, + * which we would be tracking when we free it. Waiting for idle is + * a sufficiently large hammer for now. + */ + dri_bufmgr_fake_wait_idle(bufmgr_fake); + + /* we may never have mapped this BO so it might not have any backing + * store if this happens it should be rare, but 0 the card memory + * in any case */ + if (bo_fake->backing_store) + memcpy(bo_fake->block->virtual, bo_fake->backing_store, bo->size); + else + memset(bo_fake->block->virtual, 0, bo->size); + + bo_fake->dirty = 0; + } + + bo_fake->block->fenced = 0; + bo_fake->block->on_hardware = 1; + move_to_tail(&bufmgr_fake->on_hardware, bo_fake->block); + + bo_fake->validated = GL_TRUE; + bufmgr_fake->need_fence = 1; + + return 0; +} + +static void +dri_fake_fence_validated(dri_bufmgr *bufmgr) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bufmgr; + unsigned int cookie; + + cookie = _fence_emit_internal(bufmgr_fake); + fence_blocks(bufmgr_fake, cookie); + + DBG("drm_fence_validated: 0x%08x cookie\n", cookie); +} + +static void +dri_fake_destroy(dri_bufmgr *bufmgr) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bufmgr; + + mmDestroy(bufmgr_fake->heap); + free(bufmgr); +} + +static int +dri_fake_emit_reloc(dri_bo *reloc_buf, + uint32_t read_domains, uint32_t write_domain, + uint32_t delta, uint32_t offset, dri_bo *target_buf) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)reloc_buf->bufmgr; + struct fake_buffer_reloc *r; + dri_bo_fake *reloc_fake = (dri_bo_fake *)reloc_buf; + dri_bo_fake *target_fake = (dri_bo_fake *)target_buf; + int i; + + assert(reloc_buf); + assert(target_buf); + + assert(target_fake->is_static || target_fake->size_accounted); + + if (reloc_fake->relocs == NULL) { + reloc_fake->relocs = malloc(sizeof(struct fake_buffer_reloc) * + MAX_RELOCS); + } + + r = &reloc_fake->relocs[reloc_fake->nr_relocs++]; + + assert(reloc_fake->nr_relocs <= MAX_RELOCS); + + dri_bo_reference(target_buf); + + r->target_buf = target_buf; + r->offset = offset; + r->last_target_offset = target_buf->offset; + r->delta = delta; + r->read_domains = read_domains; + r->write_domain = write_domain; + + if (bufmgr_fake->debug) { + /* Check that a conflicting relocation hasn't already been emitted. */ + for (i = 0; i < reloc_fake->nr_relocs - 1; i++) { + struct fake_buffer_reloc *r2 = &reloc_fake->relocs[i]; + + assert(r->offset != r2->offset); + } + } + + return 0; +} + +/** + * Incorporates the validation flags associated with each relocation into + * the combined validation flags for the buffer on this batchbuffer submission. + */ +static void +dri_fake_calculate_domains(dri_bo *bo) +{ + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + int i; + + for (i = 0; i < bo_fake->nr_relocs; i++) { + struct fake_buffer_reloc *r = &bo_fake->relocs[i]; + dri_bo_fake *target_fake = (dri_bo_fake *)r->target_buf; + + /* Do the same for the tree of buffers we depend on */ + dri_fake_calculate_domains(r->target_buf); + + target_fake->read_domains |= r->read_domains; + if (target_fake->write_domain != 0) + target_fake->write_domain = r->write_domain; + } +} + + +static int +dri_fake_reloc_and_validate_buffer(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + int i, ret; + + assert(bo_fake->map_count == 0); + + for (i = 0; i < bo_fake->nr_relocs; i++) { + struct fake_buffer_reloc *r = &bo_fake->relocs[i]; + dri_bo_fake *target_fake = (dri_bo_fake *)r->target_buf; + uint32_t reloc_data; + + /* Validate the target buffer if that hasn't been done. */ + if (!target_fake->validated) { + ret = dri_fake_reloc_and_validate_buffer(r->target_buf); + if (ret != 0) { + if (bo->virtual != NULL) + dri_bo_unmap(bo); + return ret; + } + } + + /* Calculate the value of the relocation entry. */ + if (r->target_buf->offset != r->last_target_offset) { + reloc_data = r->target_buf->offset + r->delta; + + if (bo->virtual == NULL) + dri_bo_map(bo, GL_TRUE); + + *(uint32_t *)(bo->virtual + r->offset) = reloc_data; + + r->last_target_offset = r->target_buf->offset; + } + } + + if (bo->virtual != NULL) + dri_bo_unmap(bo); + + if (bo_fake->write_domain != 0) { + if (!(bo_fake->flags & (BM_NO_BACKING_STORE|BM_PINNED))) { + if (bo_fake->backing_store == 0) + alloc_backing_store(bo); + + bo_fake->card_dirty = 1; + } + bufmgr_fake->performed_rendering = GL_TRUE; + } + + return dri_fake_bo_validate(bo); +} + +static void * +dri_fake_process_relocs(dri_bo *batch_buf) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)batch_buf->bufmgr; + dri_bo_fake *batch_fake = (dri_bo_fake *)batch_buf; + int ret; + int retry_count = 0; + + bufmgr_fake->performed_rendering = GL_FALSE; + + dri_fake_calculate_domains(batch_buf); + + batch_fake->read_domains = DRM_GEM_DOMAIN_I915_COMMAND; + + /* we've ran out of RAM so blow the whole lot away and retry */ + restart: + ret = dri_fake_reloc_and_validate_buffer(batch_buf); + if (bufmgr_fake->fail == 1) { + if (retry_count == 0) { + retry_count++; + dri_fake_kick_all(bufmgr_fake); + bufmgr_fake->fail = 0; + goto restart; + } else /* dump out the memory here */ + mmDumpMemInfo(bufmgr_fake->heap); + } + + assert(ret == 0); + + bufmgr_fake->current_total_size = 0; + return NULL; +} + +static void +dri_bo_fake_post_submit(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + int i; + + for (i = 0; i < bo_fake->nr_relocs; i++) { + struct fake_buffer_reloc *r = &bo_fake->relocs[i]; + dri_bo_fake *target_fake = (dri_bo_fake *)r->target_buf; + + if (target_fake->validated) + dri_bo_fake_post_submit(r->target_buf); + + DBG("%s@0x%08x + 0x%08x -> %s@0x%08x + 0x%08x\n", + bo_fake->name, (uint32_t)bo->offset, r->offset, + target_fake->name, (uint32_t)r->target_buf->offset, r->delta); + } + + assert(bo_fake->map_count == 0); + bo_fake->validated = GL_FALSE; + bo_fake->read_domains = 0; + bo_fake->write_domain = 0; +} + + +static void +dri_fake_post_submit(dri_bo *batch_buf) +{ + dri_fake_fence_validated(batch_buf->bufmgr); + + dri_bo_fake_post_submit(batch_buf); +} + +static int +dri_fake_check_aperture_space(dri_bo *bo) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; + dri_bo_fake *bo_fake = (dri_bo_fake *)bo; + GLuint sz; + + sz = (bo->size + bo_fake->alignment - 1) & ~(bo_fake->alignment - 1); + + if (bo_fake->size_accounted || bo_fake->is_static) + return 0; + + if (bufmgr_fake->current_total_size + sz > bufmgr_fake->size) { + DBG("check_space: %s bo %d %d overflowed bufmgr size %d\n", bo_fake->name, bo_fake->id, sz, bufmgr_fake->size); + return -1; + } + + bufmgr_fake->current_total_size += sz; + bo_fake->size_accounted = 1; + DBG("drm_check_space: buf %d, %s %d %d\n", bo_fake->id, bo_fake->name, bo->size, bufmgr_fake->current_total_size); + return 0; +} + +dri_bufmgr * +dri_bufmgr_fake_init(unsigned long low_offset, void *low_virtual, + unsigned long size, + unsigned int (*fence_emit)(void *private), + int (*fence_wait)(void *private, unsigned int cookie), + void *driver_priv) +{ + dri_bufmgr_fake *bufmgr_fake; + + bufmgr_fake = calloc(1, sizeof(*bufmgr_fake)); + + /* Initialize allocator */ + make_empty_list(&bufmgr_fake->fenced); + make_empty_list(&bufmgr_fake->on_hardware); + make_empty_list(&bufmgr_fake->lru); + + bufmgr_fake->low_offset = low_offset; + bufmgr_fake->virtual = low_virtual; + bufmgr_fake->size = size; + bufmgr_fake->heap = mmInit(low_offset, size); + + /* Hook in methods */ + bufmgr_fake->bufmgr.bo_alloc = dri_fake_bo_alloc; + bufmgr_fake->bufmgr.bo_alloc_static = dri_fake_bo_alloc_static; + bufmgr_fake->bufmgr.bo_reference = dri_fake_bo_reference; + bufmgr_fake->bufmgr.bo_unreference = dri_fake_bo_unreference; + bufmgr_fake->bufmgr.bo_map = dri_fake_bo_map; + bufmgr_fake->bufmgr.bo_unmap = dri_fake_bo_unmap; + bufmgr_fake->bufmgr.bo_wait_rendering = dri_fake_bo_wait_rendering; + bufmgr_fake->bufmgr.destroy = dri_fake_destroy; + bufmgr_fake->bufmgr.emit_reloc = dri_fake_emit_reloc; + bufmgr_fake->bufmgr.process_relocs = dri_fake_process_relocs; + bufmgr_fake->bufmgr.post_submit = dri_fake_post_submit; + bufmgr_fake->bufmgr.check_aperture_space = dri_fake_check_aperture_space; + bufmgr_fake->bufmgr.debug = GL_FALSE; + + bufmgr_fake->fence_emit = fence_emit; + bufmgr_fake->fence_wait = fence_wait; + bufmgr_fake->driver_priv = driver_priv; + + return &bufmgr_fake->bufmgr; +} + diff --git a/libdrm/intel/intel_bufmgr_fake.h b/libdrm/intel/intel_bufmgr_fake.h new file mode 100644 index 00000000..bc7e59e6 --- /dev/null +++ b/libdrm/intel/intel_bufmgr_fake.h @@ -0,0 +1,50 @@ +/************************************************************************** + * + * Copyright © 2007 Intel Corporation + * 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 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. + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * + **************************************************************************/ +/* + * Authors: Thomas Hellström + * Keith Whitwell + * Eric Anholt + */ + +#ifndef _INTEL_BUFMGR_FAKE_H_ +#define _INTEL_BUFMGR_FAKE_H_ + +void dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr); +dri_bufmgr *dri_bufmgr_fake_init(unsigned long low_offset, void *low_virtual, + unsigned long size, + unsigned int (*fence_emit)(void *private), + int (*fence_wait)(void *private, + unsigned int cookie), + void *driver_priv); +void dri_bo_fake_disable_backing_store(dri_bo *bo, + void (*invalidate_cb)(dri_bo *bo, + void *ptr), + void *ptr); +#endif /* _INTEL_BUFMGR_FAKE_H_ */ + diff --git a/libdrm/intel/intel_bufmgr_gem.c b/libdrm/intel/intel_bufmgr_gem.c new file mode 100644 index 00000000..3c1c3157 --- /dev/null +++ b/libdrm/intel/intel_bufmgr_gem.c @@ -0,0 +1,847 @@ +/************************************************************************** + * + * Copyright © 2007 Red Hat Inc. + * Copyright © 2007 Intel Corporation + * 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 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. + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * + **************************************************************************/ +/* + * Authors: Thomas Hellström + * Keith Whitwell + * Eric Anholt + * Dave Airlie + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "errno.h" +#include "mtypes.h" +#include "dri_bufmgr.h" +#include "string.h" +#include "imports.h" + +#include "i915_drm.h" + +#include "intel_bufmgr_gem.h" + +#define DBG(...) do { \ + if (bufmgr_gem->bufmgr.debug) \ + fprintf(stderr, __VA_ARGS__); \ +} while (0) + +struct intel_validate_entry { + dri_bo *bo; + struct drm_i915_op_arg bo_arg; +}; + +struct dri_gem_bo_bucket_entry { + uint32_t gem_handle; + uint32_t last_offset; + struct dri_gem_bo_bucket_entry *next; +}; + +struct dri_gem_bo_bucket { + struct dri_gem_bo_bucket_entry *head; + struct dri_gem_bo_bucket_entry **tail; + /** + * Limit on the number of entries in this bucket. + * + * 0 means that this caching at this bucket size is disabled. + * -1 means that there is no limit to caching at this size. + */ + int max_entries; + int num_entries; +}; + +/* Arbitrarily chosen, 16 means that the maximum size we'll cache for reuse + * is 1 << 16 pages, or 256MB. + */ +#define INTEL_GEM_BO_BUCKETS 16 +typedef struct _dri_bufmgr_gem { + dri_bufmgr bufmgr; + + int fd; + + uint32_t max_relocs; + + struct drm_i915_gem_exec_object *exec_objects; + dri_bo **exec_bos; + int exec_size; + int exec_count; + + /** Array of lists of cached gem objects of power-of-two sizes */ + struct dri_gem_bo_bucket cache_bucket[INTEL_GEM_BO_BUCKETS]; + + struct drm_i915_gem_execbuffer exec_arg; +} dri_bufmgr_gem; + +typedef struct _dri_bo_gem { + dri_bo bo; + + int refcount; + GLboolean mapped; + uint32_t gem_handle; + const char *name; + + /** + * Index of the buffer within the validation list while preparing a + * batchbuffer execution. + */ + int validate_index; + + /** + * Tracks whether set_domain to CPU is current + * Set when set_domain has been called + * Cleared when a batch has been submitted + */ + GLboolean cpu_domain_set; + + /** Array passed to the DRM containing relocation information. */ + struct drm_i915_gem_relocation_entry *relocs; + /** Array of bos corresponding to relocs[i].target_handle */ + dri_bo **reloc_target_bo; + /** Number of entries in relocs */ + int reloc_count; + /** Mapped address for the buffer */ + void *virtual; +} dri_bo_gem; + +static int +logbase2(int n) +{ + GLint i = 1; + GLint log2 = 0; + + while (n > i) { + i *= 2; + log2++; + } + + return log2; +} + +static struct dri_gem_bo_bucket * +dri_gem_bo_bucket_for_size(dri_bufmgr_gem *bufmgr_gem, unsigned long size) +{ + int i; + + /* We only do buckets in power of two increments */ + if ((size & (size - 1)) != 0) + return NULL; + + /* We should only see sizes rounded to pages. */ + assert((size % 4096) == 0); + + /* We always allocate in units of pages */ + i = ffs(size / 4096) - 1; + if (i >= INTEL_GEM_BO_BUCKETS) + return NULL; + + return &bufmgr_gem->cache_bucket[i]; +} + + +static void dri_gem_dump_validation_list(dri_bufmgr_gem *bufmgr_gem) +{ + int i, j; + + for (i = 0; i < bufmgr_gem->exec_count; i++) { + dri_bo *bo = bufmgr_gem->exec_bos[i]; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + + if (bo_gem->relocs == NULL) { + DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle, bo_gem->name); + continue; + } + + for (j = 0; j < bo_gem->reloc_count; j++) { + dri_bo *target_bo = bo_gem->reloc_target_bo[j]; + dri_bo_gem *target_gem = (dri_bo_gem *)target_bo; + + DBG("%2d: %d (%s)@0x%08llx -> %d (%s)@0x%08lx + 0x%08x\n", + i, + bo_gem->gem_handle, bo_gem->name, bo_gem->relocs[j].offset, + target_gem->gem_handle, target_gem->name, target_bo->offset, + bo_gem->relocs[j].delta); + } + } +} + +/** + * Adds the given buffer to the list of buffers to be validated (moved into the + * appropriate memory type) with the next batch submission. + * + * If a buffer is validated multiple times in a batch submission, it ends up + * with the intersection of the memory type flags and the union of the + * access flags. + */ +static void +intel_add_validate_buffer(dri_bo *bo) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + int index; + + if (bo_gem->validate_index != -1) + return; + + /* Extend the array of validation entries as necessary. */ + if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) { + int new_size = bufmgr_gem->exec_size * 2; + + if (new_size == 0) + new_size = 5; + + bufmgr_gem->exec_objects = + realloc(bufmgr_gem->exec_objects, + sizeof(*bufmgr_gem->exec_objects) * new_size); + bufmgr_gem->exec_bos = + realloc(bufmgr_gem->exec_bos, + sizeof(*bufmgr_gem->exec_bos) * new_size); + bufmgr_gem->exec_size = new_size; + } + + index = bufmgr_gem->exec_count; + bo_gem->validate_index = index; + /* Fill in array entry */ + bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle; + bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count; + bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs; + bufmgr_gem->exec_objects[index].alignment = 0; + bufmgr_gem->exec_objects[index].offset = 0; + bufmgr_gem->exec_bos[index] = bo; + dri_bo_reference(bo); + bufmgr_gem->exec_count++; +} + + +#define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \ + sizeof(uint32_t)) + +static int +intel_setup_reloc_list(dri_bo *bo) +{ + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + + bo_gem->relocs = malloc(bufmgr_gem->max_relocs * + sizeof(struct drm_i915_gem_relocation_entry)); + bo_gem->reloc_target_bo = malloc(bufmgr_gem->max_relocs * sizeof(dri_bo *)); + + return 0; +} + +static dri_bo * +dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, + unsigned long size, unsigned int alignment, + uint64_t location_mask) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr; + dri_bo_gem *bo_gem; + unsigned int page_size = getpagesize(); + int ret; + struct dri_gem_bo_bucket *bucket; + GLboolean alloc_from_cache = GL_FALSE; + + bo_gem = calloc(1, sizeof(*bo_gem)); + if (!bo_gem) + return NULL; + + /* Round the allocated size up to a power of two number of pages. */ + bo_gem->bo.size = 1 << logbase2(size); + if (bo_gem->bo.size < page_size) + bo_gem->bo.size = page_size; + bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo_gem->bo.size); + + /* If we don't have caching at this size, don't actually round the + * allocation up. + */ + if (bucket == NULL || bucket->max_entries == 0) { + bo_gem->bo.size = size; + if (bo_gem->bo.size < page_size) + bo_gem->bo.size = page_size; + } + + /* Get a buffer out of the cache if available */ + if (bucket != NULL && bucket->num_entries > 0) { + struct dri_gem_bo_bucket_entry *entry = bucket->head; + struct drm_i915_gem_busy busy; + + busy.handle = entry->gem_handle; + ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy); + alloc_from_cache = (ret == 0 && busy.busy == 0); + + if (alloc_from_cache) { + bucket->head = entry->next; + if (entry->next == NULL) + bucket->tail = &bucket->head; + bucket->num_entries--; + + bo_gem->gem_handle = entry->gem_handle; + bo_gem->bo.offset = entry->last_offset; + free(entry); + } + } + + if (!alloc_from_cache) { + struct drm_gem_create create; + + memset(&create, 0, sizeof(create)); + create.size = bo_gem->bo.size; + + ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CREATE, &create); + bo_gem->gem_handle = create.handle; + if (ret != 0) { + free(bo_gem); + return NULL; + } + } + + bo_gem->bo.virtual = NULL; + bo_gem->bo.bufmgr = bufmgr; + bo_gem->name = name; + bo_gem->refcount = 1; + bo_gem->validate_index = -1; + + DBG("bo_create: buf %d (%s) %ldb\n", + bo_gem->gem_handle, bo_gem->name, size); + + return &bo_gem->bo; +} + +/* Our GEM backend doesn't allow creation of static buffers, as that requires + * privelege for the non-fake case, and the lock in the fake case where we were + * working around the X Server not creating buffers and passing handles to us. + */ +static dri_bo * +dri_gem_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, + unsigned long offset, unsigned long size, void *virtual, + uint64_t location_mask) +{ + return NULL; +} + +/** + * Returns a dri_bo wrapping the given buffer object handle. + * + * This can be used when one application needs to pass a buffer object + * to another. + */ +dri_bo * +intel_gem_bo_create_from_handle(dri_bufmgr *bufmgr, const char *name, + unsigned int handle) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr; + dri_bo_gem *bo_gem; + int ret; + struct drm_gem_open open_arg; + + bo_gem = calloc(1, sizeof(*bo_gem)); + if (!bo_gem) + return NULL; + + memset(&open_arg, 0, sizeof(open_arg)); + open_arg.name = handle; + ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_OPEN, &open_arg); + if (ret != 0) { + fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n", + name, handle, strerror(-ret)); + free(bo_gem); + return NULL; + } + bo_gem->bo.size = open_arg.size; + bo_gem->bo.offset = 0; + bo_gem->bo.virtual = NULL; + bo_gem->bo.bufmgr = bufmgr; + bo_gem->name = name; + bo_gem->refcount = 1; + bo_gem->validate_index = -1; + bo_gem->gem_handle = open_arg.handle; + + DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name); + + return &bo_gem->bo; +} + +static void +dri_gem_bo_reference(dri_bo *bo) +{ + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + + bo_gem->refcount++; +} + +static void +dri_gem_bo_unreference(dri_bo *bo) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + + if (!bo) + return; + + if (--bo_gem->refcount == 0) { + struct dri_gem_bo_bucket *bucket; + int ret; + + if (bo_gem->mapped) + munmap (bo_gem->virtual, bo->size); + + if (bo_gem->relocs != NULL) { + int i; + + /* Unreference all the target buffers */ + for (i = 0; i < bo_gem->reloc_count; i++) + dri_bo_unreference(bo_gem->reloc_target_bo[i]); + free(bo_gem->reloc_target_bo); + free(bo_gem->relocs); + } + + bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo->size); + /* Put the buffer into our internal cache for reuse if we can. */ + if (bucket != NULL && + (bucket->max_entries == -1 || + (bucket->max_entries > 0 && + bucket->num_entries < bucket->max_entries))) + { + struct dri_gem_bo_bucket_entry *entry; + + entry = calloc(1, sizeof(*entry)); + entry->gem_handle = bo_gem->gem_handle; + entry->last_offset = bo->offset; + + entry->next = NULL; + *bucket->tail = entry; + bucket->tail = &entry->next; + bucket->num_entries++; + } else { + struct drm_gem_close close; + + /* Close this object */ + close.handle = bo_gem->gem_handle; + ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close); + if (ret != 0) { + fprintf(stderr, + "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n", + bo_gem->gem_handle, bo_gem->name, strerror(-ret)); + } + } + + DBG("bo_unreference final: %d (%s)\n", + bo_gem->gem_handle, bo_gem->name); + + free(bo); + return; + } +} + +static int +dri_gem_bo_map(dri_bo *bo, GLboolean write_enable) +{ + dri_bufmgr_gem *bufmgr_gem; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + struct drm_gem_set_domain set_domain; + int ret; + + bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + + /* Allow recursive mapping. Mesa may recursively map buffers with + * nested display loops. + */ + if (!bo_gem->mapped) { + + assert(bo->virtual == NULL); + + DBG("bo_map: %d (%s)\n", bo_gem->gem_handle, bo_gem->name); + + if (bo_gem->virtual == NULL) { + struct drm_gem_mmap mmap_arg; + + memset(&mmap_arg, 0, sizeof(mmap_arg)); + mmap_arg.handle = bo_gem->gem_handle; + mmap_arg.offset = 0; + mmap_arg.size = bo->size; + ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_MMAP, &mmap_arg); + if (ret != 0) { + fprintf(stderr, "%s:%d: Error mapping buffer %d (%s): %s .\n", + __FILE__, __LINE__, + bo_gem->gem_handle, bo_gem->name, strerror(errno)); + } + bo_gem->virtual = (void *)(uintptr_t)mmap_arg.addr_ptr; + } + bo->virtual = bo_gem->virtual; + bo_gem->mapped = GL_TRUE; + DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name, bo_gem->virtual); + } + + if (!bo_gem->cpu_domain_set) { + set_domain.handle = bo_gem->gem_handle; + set_domain.read_domains = DRM_GEM_DOMAIN_CPU; + set_domain.write_domain = write_enable ? DRM_GEM_DOMAIN_CPU : 0; + ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_GEM_SET_DOMAIN, &set_domain); + if (ret != 0) { + fprintf (stderr, "%s:%d: Error setting memory domains %d (%08x %08x): %s .\n", + __FILE__, __LINE__, + bo_gem->gem_handle, set_domain.read_domains, set_domain.write_domain, + strerror (errno)); + } + bo_gem->cpu_domain_set = GL_TRUE; + } + + return 0; +} + +static int +dri_gem_bo_unmap(dri_bo *bo) +{ + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + + if (bo == NULL) + return 0; + + assert(bo_gem->mapped); + + return 0; +} + +static int +dri_gem_bo_subdata (dri_bo *bo, unsigned long offset, + unsigned long size, const void *data) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + struct drm_gem_pwrite pwrite; + int ret; + + memset (&pwrite, 0, sizeof (pwrite)); + pwrite.handle = bo_gem->gem_handle; + pwrite.offset = offset; + pwrite.size = size; + pwrite.data_ptr = (uint64_t) (uintptr_t) data; + ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_GEM_PWRITE, &pwrite); + if (ret != 0) { + fprintf (stderr, "%s:%d: Error writing data to buffer %d: (%d %d) %s .\n", + __FILE__, __LINE__, + bo_gem->gem_handle, (int) offset, (int) size, + strerror (errno)); + } + return 0; +} + +static int +dri_gem_bo_get_subdata (dri_bo *bo, unsigned long offset, + unsigned long size, void *data) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + struct drm_gem_pread pread; + int ret; + + memset (&pread, 0, sizeof (pread)); + pread.handle = bo_gem->gem_handle; + pread.offset = offset; + pread.size = size; + pread.data_ptr = (uint64_t) (uintptr_t) data; + ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_GEM_PREAD, &pread); + if (ret != 0) { + fprintf (stderr, "%s:%d: Error reading data from buffer %d: (%d %d) %s .\n", + __FILE__, __LINE__, + bo_gem->gem_handle, (int) offset, (int) size, + strerror (errno)); + } + return 0; +} + +static void +dri_gem_bo_wait_rendering(dri_bo *bo) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + struct drm_gem_set_domain set_domain; + int ret; + + set_domain.handle = bo_gem->gem_handle; + set_domain.read_domains = DRM_GEM_DOMAIN_CPU; + set_domain.write_domain = 0; + ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_GEM_SET_DOMAIN, &set_domain); + if (ret != 0) { + fprintf (stderr, "%s:%d: Error setting memory domains %d (%08x %08x): %s .\n", + __FILE__, __LINE__, + bo_gem->gem_handle, set_domain.read_domains, set_domain.write_domain, + strerror (errno)); + } +} + +static void +dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr; + int i; + + free(bufmgr_gem->exec_objects); + free(bufmgr_gem->exec_bos); + + /* Free any cached buffer objects we were going to reuse */ + for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) { + struct dri_gem_bo_bucket *bucket = &bufmgr_gem->cache_bucket[i]; + struct dri_gem_bo_bucket_entry *entry; + + while ((entry = bucket->head) != NULL) { + struct drm_gem_close close; + int ret; + + bucket->head = entry->next; + if (entry->next == NULL) + bucket->tail = &bucket->head; + bucket->num_entries--; + + /* Close this object */ + close.handle = entry->gem_handle; + ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close); + if (ret != 0) { + fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %s\n", + strerror(-ret)); + } + + free(entry); + } + } + + free(bufmgr); +} + +/** + * Adds the target buffer to the validation list and adds the relocation + * to the reloc_buffer's relocation list. + * + * The relocation entry at the given offset must already contain the + * precomputed relocation value, because the kernel will optimize out + * the relocation entry write when the buffer hasn't moved from the + * last known offset in target_bo. + */ +static int +dri_gem_emit_reloc(dri_bo *bo, uint32_t read_domains, uint32_t write_domain, + uint32_t delta, uint32_t offset, dri_bo *target_bo) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + dri_bo_gem *target_bo_gem = (dri_bo_gem *)target_bo; + + /* Create a new relocation list if needed */ + if (bo_gem->relocs == NULL) + intel_setup_reloc_list(bo); + + /* Check overflow */ + assert(bo_gem->reloc_count < bufmgr_gem->max_relocs); + + /* Check args */ + assert (offset <= bo->size - 4); + assert ((write_domain & (write_domain-1)) == 0); + + bo_gem->relocs[bo_gem->reloc_count].offset = offset; + bo_gem->relocs[bo_gem->reloc_count].delta = delta; + bo_gem->relocs[bo_gem->reloc_count].target_handle = + target_bo_gem->gem_handle; + bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains; + bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain; + bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset; + + bo_gem->reloc_target_bo[bo_gem->reloc_count] = target_bo; + dri_bo_reference(target_bo); + + bo_gem->reloc_count++; + return 0; +} + +/** + * Walk the tree of relocations rooted at BO and accumulate the list of + * validations to be performed and update the relocation buffers with + * index values into the validation list. + */ +static void +dri_gem_bo_process_reloc(dri_bo *bo) +{ + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + int i; + + if (bo_gem->relocs == NULL) + return; + + for (i = 0; i < bo_gem->reloc_count; i++) { + dri_bo *target_bo = bo_gem->reloc_target_bo[i]; + + /* Continue walking the tree depth-first. */ + dri_gem_bo_process_reloc(target_bo); + + /* Add the target to the validate list */ + intel_add_validate_buffer(target_bo); + } +} + +static void * +dri_gem_process_reloc(dri_bo *batch_buf) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *) batch_buf->bufmgr; + + /* Update indices and set up the validate list. */ + dri_gem_bo_process_reloc(batch_buf); + + /* Add the batch buffer to the validation list. There are no relocations + * pointing to it. + */ + intel_add_validate_buffer(batch_buf); + + bufmgr_gem->exec_arg.buffers_ptr = (uintptr_t)bufmgr_gem->exec_objects; + bufmgr_gem->exec_arg.buffer_count = bufmgr_gem->exec_count; + bufmgr_gem->exec_arg.batch_start_offset = 0; + bufmgr_gem->exec_arg.batch_len = 0; /* written in intel_exec_ioctl */ + + return &bufmgr_gem->exec_arg; +} + +static void +intel_update_buffer_offsets (dri_bufmgr_gem *bufmgr_gem) +{ + int i; + + for (i = 0; i < bufmgr_gem->exec_count; i++) { + dri_bo *bo = bufmgr_gem->exec_bos[i]; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + + /* Update the buffer offset */ + if (bufmgr_gem->exec_objects[i].offset != bo->offset) { + DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n", + bo_gem->gem_handle, bo_gem->name, bo->offset, + bufmgr_gem->exec_objects[i].offset); + bo->offset = bufmgr_gem->exec_objects[i].offset; + } + } +} + +static void +dri_gem_post_submit(dri_bo *batch_buf) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)batch_buf->bufmgr; + int i; + + intel_update_buffer_offsets (bufmgr_gem); + + if (bufmgr_gem->bufmgr.debug) + dri_gem_dump_validation_list(bufmgr_gem); + + for (i = 0; i < bufmgr_gem->exec_count; i++) { + dri_bo *bo = bufmgr_gem->exec_bos[i]; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + + /* Need to call set_domain on next bo_map */ + bo_gem->cpu_domain_set = GL_FALSE; + + /* Disconnect the buffer from the validate list */ + bo_gem->validate_index = -1; + dri_bo_unreference(bo); + bufmgr_gem->exec_bos[i] = NULL; + } + bufmgr_gem->exec_count = 0; +} + +/** + * Enables unlimited caching of buffer objects for reuse. + * + * This is potentially very memory expensive, as the cache at each bucket + * size is only bounded by how many buffers of that size we've managed to have + * in flight at once. + */ +void +intel_gem_enable_bo_reuse(dri_bufmgr *bufmgr) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr; + int i; + + for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) { + bufmgr_gem->cache_bucket[i].max_entries = -1; + } +} + +/* + * + */ +static int +dri_gem_check_aperture_space(dri_bo *bo) +{ + return 0; +} + +/** + * Initializes the GEM buffer manager, which uses the kernel to allocate, map, + * and manage map buffer objections. + * + * \param fd File descriptor of the opened DRM device. + */ +dri_bufmgr * +intel_bufmgr_gem_init(int fd, int batch_size) +{ + dri_bufmgr_gem *bufmgr_gem; + int i; + + bufmgr_gem = calloc(1, sizeof(*bufmgr_gem)); + bufmgr_gem->fd = fd; + + /* Let's go with one relocation per every 2 dwords (but round down a bit + * since a power of two will mean an extra page allocation for the reloc + * buffer). + * + * Every 4 was too few for the blender benchmark. + */ + bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2; + + bufmgr_gem->bufmgr.bo_alloc = dri_gem_bo_alloc; + bufmgr_gem->bufmgr.bo_alloc_static = dri_gem_bo_alloc_static; + bufmgr_gem->bufmgr.bo_reference = dri_gem_bo_reference; + bufmgr_gem->bufmgr.bo_unreference = dri_gem_bo_unreference; + bufmgr_gem->bufmgr.bo_map = dri_gem_bo_map; + bufmgr_gem->bufmgr.bo_unmap = dri_gem_bo_unmap; + bufmgr_gem->bufmgr.bo_subdata = dri_gem_bo_subdata; + bufmgr_gem->bufmgr.bo_get_subdata = dri_gem_bo_get_subdata; + bufmgr_gem->bufmgr.bo_wait_rendering = dri_gem_bo_wait_rendering; + bufmgr_gem->bufmgr.destroy = dri_bufmgr_gem_destroy; + bufmgr_gem->bufmgr.emit_reloc = dri_gem_emit_reloc; + bufmgr_gem->bufmgr.process_relocs = dri_gem_process_reloc; + bufmgr_gem->bufmgr.post_submit = dri_gem_post_submit; + bufmgr_gem->bufmgr.debug = GL_FALSE; + bufmgr_gem->bufmgr.check_aperture_space = dri_gem_check_aperture_space; + /* Initialize the linked lists for BO reuse cache. */ + for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) + bufmgr_gem->cache_bucket[i].tail = &bufmgr_gem->cache_bucket[i].head; + + return &bufmgr_gem->bufmgr; +} + diff --git a/libdrm/intel/intel_bufmgr_gem.h b/libdrm/intel/intel_bufmgr_gem.h new file mode 100644 index 00000000..36caeba2 --- /dev/null +++ b/libdrm/intel/intel_bufmgr_gem.h @@ -0,0 +1,16 @@ + +#ifndef INTEL_BUFMGR_GEM_H +#define INTEL_BUFMGR_GEM_H + +#include "dri_bufmgr.h" + +extern dri_bo *intel_gem_bo_create_from_handle(dri_bufmgr *bufmgr, + const char *name, + unsigned int handle); + +dri_bufmgr *intel_bufmgr_gem_init(int fd, int batch_size); + +void +intel_gem_enable_bo_reuse(dri_bufmgr *bufmgr); + +#endif /* INTEL_BUFMGR_GEM_H */ -- cgit v1.2.3 From c4857429c716f35e1fa054d1990cae28055d96d7 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 3 Jun 2008 10:20:49 -0700 Subject: Fix and hook up bufmgr code to the build. --- configure.ac | 68 ++++++++++ libdrm/Makefile.am | 5 +- libdrm/dri_bufmgr.c | 42 +----- libdrm/dri_bufmgr.h | 54 +------- libdrm/intel/Makefile.am | 38 ++++++ libdrm/intel/intel_bufmgr.h | 94 +++++++++++++ libdrm/intel/intel_bufmgr_fake.c | 234 ++++++++++++++++---------------- libdrm/intel/intel_bufmgr_fake.h | 50 ------- libdrm/intel/intel_bufmgr_gem.c | 66 +++++---- libdrm/intel/intel_bufmgr_gem.h | 16 --- libdrm/intel/mm.c | 281 +++++++++++++++++++++++++++++++++++++++ libdrm/intel/mm.h | 88 ++++++++++++ libdrm/xf86drm.c | 2 +- libdrm/xf86drm.h | 1 + libdrm/xf86mm.h | 12 ++ 15 files changed, 752 insertions(+), 299 deletions(-) create mode 100644 libdrm/intel/Makefile.am create mode 100644 libdrm/intel/intel_bufmgr.h delete mode 100644 libdrm/intel/intel_bufmgr_fake.h delete mode 100644 libdrm/intel/intel_bufmgr_gem.h create mode 100644 libdrm/intel/mm.c create mode 100644 libdrm/intel/mm.h diff --git a/configure.ac b/configure.ac index 78203343..a8855684 100644 --- a/configure.ac +++ b/configure.ac @@ -35,9 +35,77 @@ AC_SYS_LARGEFILE pkgconfigdir=${libdir}/pkgconfig AC_SUBST(pkgconfigdir) + +dnl =========================================================================== +dnl check compiler flags +AC_DEFUN([LIBDRM_CC_TRY_FLAG], [ + AC_MSG_CHECKING([whether $CC supports $1]) + + libdrm_save_CFLAGS="$CFLAGS" + CFLAGS="$CFLAGS $1" + + AC_COMPILE_IFELSE([ ], [libdrm_cc_flag=yes], [libdrm_cc_flag=no]) + CFLAGS="$libdrm_save_CFLAGS" + + if test "x$libdrm_cc_flag" = "xyes"; then + ifelse([$2], , :, [$2]) + else + ifelse([$3], , :, [$3]) + fi + AC_MSG_RESULT([$libdrm_cc_flag]) +]) + +dnl Use lots of warning flags with with gcc and compatible compilers + +dnl Note: if you change the following variable, the cache is automatically +dnl skipped and all flags rechecked. So there's no need to do anything +dnl else. If for any reason you need to force a recheck, just change +dnl MAYBE_WARN in an ignorable way (like adding whitespace) + +MAYBE_WARN="-Wall -Wextra \ +-Wsign-compare -Werror-implicit-function-declaration \ +-Wpointer-arith -Wwrite-strings -Wstrict-prototypes \ +-Wmissing-prototypes -Wmissing-declarations -Wnested-externs \ +-Wpacked -Wswitch-enum -Wmissing-format-attribute \ +-Wstrict-aliasing=2 -Winit-self -Wunsafe-loop-optimizations \ +-Wdeclaration-after-statement -Wold-style-definition \ +-Wno-missing-field-initializers -Wno-unused-parameter \ +-Wno-attributes -Wno-long-long -Winline" + +# invalidate cached value if MAYBE_WARN has changed +if test "x$libdrm_cv_warn_maybe" != "x$MAYBE_WARN"; then + unset libdrm_cv_warn_cflags +fi +AC_CACHE_CHECK([for supported warning flags], libdrm_cv_warn_cflags, [ + echo + WARN_CFLAGS="" + + # Some warning options are not supported by all versions of + # gcc, so test all desired options against the current + # compiler. + # + # Note that there are some order dependencies + # here. Specifically, an option that disables a warning will + # have no net effect if a later option then enables that + # warnings, (perhaps implicitly). So we put some grouped + # options (-Wall and -Wextra) up front and the -Wno options + # last. + + for W in $MAYBE_WARN; do + LIBDRM_CC_TRY_FLAG([$W], [WARN_CFLAGS="$WARN_CFLAGS $W"]) + done + + libdrm_cv_warn_cflags=$WARN_CFLAGS + libdrm_cv_warn_maybe=$MAYBE_WARN + + AC_MSG_CHECKING([which warning flags were supported])]) +WARN_CFLAGS="$libdrm_cv_warn_cflags" + +AC_SUBST(WARN_CFLAGS) AC_OUTPUT([ Makefile libdrm/Makefile + libdrm/intel/Makefile shared-core/Makefile tests/Makefile libdrm.pc]) diff --git a/libdrm/Makefile.am b/libdrm/Makefile.am index e7e07e47..c8530286 100644 --- a/libdrm/Makefile.am +++ b/libdrm/Makefile.am @@ -18,14 +18,17 @@ # 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. +SUBDIRS = intel + libdrm_la_LTLIBRARIES = libdrm.la libdrm_ladir = $(libdir) libdrm_la_LDFLAGS = -version-number 2:3:0 -no-undefined AM_CFLAGS = -I$(top_srcdir)/shared-core libdrm_la_SOURCES = xf86drm.c xf86drmHash.c xf86drmRandom.c xf86drmSL.c +libdrm_la_DEPENDENCIES = intel/libdrm_intel.la libdrmincludedir = ${includedir} -libdrminclude_HEADERS = xf86drm.h xf86mm.h +libdrminclude_HEADERS = xf86drm.h xf86mm.h dri_bufmgr.h EXTRA_DIST = ChangeLog TODO diff --git a/libdrm/dri_bufmgr.c b/libdrm/dri_bufmgr.c index be2a7b74..7657df61 100644 --- a/libdrm/dri_bufmgr.c +++ b/libdrm/dri_bufmgr.c @@ -28,7 +28,6 @@ #include #include #include -#include "mtypes.h" #include "dri_bufmgr.h" /** @file dri_bufmgr.c @@ -38,29 +37,9 @@ dri_bo * dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size, - unsigned int alignment, uint64_t location_mask) + unsigned int alignment) { - assert((location_mask & ~(DRM_BO_FLAG_MEM_LOCAL | DRM_BO_FLAG_MEM_TT | - DRM_BO_FLAG_MEM_VRAM | DRM_BO_FLAG_MEM_PRIV0 | - DRM_BO_FLAG_MEM_PRIV1 | DRM_BO_FLAG_MEM_PRIV2 | - DRM_BO_FLAG_MEM_PRIV3 | DRM_BO_FLAG_MEM_PRIV4 | - DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED)) == 0); - return bufmgr->bo_alloc(bufmgr, name, size, alignment, location_mask); -} - -dri_bo * -dri_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, unsigned long offset, - unsigned long size, void *virtual, - uint64_t location_mask) -{ - assert((location_mask & ~(DRM_BO_FLAG_MEM_LOCAL | DRM_BO_FLAG_MEM_TT | - DRM_BO_FLAG_MEM_VRAM | DRM_BO_FLAG_MEM_PRIV0 | - DRM_BO_FLAG_MEM_PRIV1 | DRM_BO_FLAG_MEM_PRIV2 | - DRM_BO_FLAG_MEM_PRIV3 | - DRM_BO_FLAG_MEM_PRIV4)) == 0); - - return bufmgr->bo_alloc_static(bufmgr, name, offset, size, virtual, - location_mask); + return bufmgr->bo_alloc(bufmgr, name, size, alignment); } void @@ -79,7 +58,7 @@ dri_bo_unreference(dri_bo *bo) } int -dri_bo_map(dri_bo *buf, GLboolean write_enable) +dri_bo_map(dri_bo *buf, int write_enable) { return buf->bufmgr->bo_map(buf, write_enable); } @@ -100,7 +79,7 @@ dri_bo_subdata(dri_bo *bo, unsigned long offset, if (size == 0 || data == NULL) return 0; - ret = dri_bo_map(bo, GL_TRUE); + ret = dri_bo_map(bo, 1); if (ret) return ret; memcpy((unsigned char *)bo->virtual + offset, data, size); @@ -119,7 +98,7 @@ dri_bo_get_subdata(dri_bo *bo, unsigned long offset, if (size == 0 || data == NULL) return 0; - ret = dri_bo_map(bo, GL_FALSE); + ret = dri_bo_map(bo, 0); if (ret) return ret; memcpy(data, (unsigned char *)bo->virtual + offset, size); @@ -139,15 +118,6 @@ dri_bufmgr_destroy(dri_bufmgr *bufmgr) bufmgr->destroy(bufmgr); } - -int dri_emit_reloc(dri_bo *reloc_buf, - uint32_t read_domains, uint32_t write_domain, - uint32_t delta, uint32_t offset, dri_bo *target_buf) -{ - return reloc_buf->bufmgr->emit_reloc(reloc_buf, read_domains, write_domain, - delta, offset, target_buf); -} - void *dri_process_relocs(dri_bo *batch_buf) { return batch_buf->bufmgr->process_relocs(batch_buf); @@ -159,7 +129,7 @@ void dri_post_submit(dri_bo *batch_buf) } void -dri_bufmgr_set_debug(dri_bufmgr *bufmgr, GLboolean enable_debug) +dri_bufmgr_set_debug(dri_bufmgr *bufmgr, int enable_debug) { bufmgr->debug = enable_debug; } diff --git a/libdrm/dri_bufmgr.h b/libdrm/dri_bufmgr.h index 1abca08c..a5ae6c0f 100644 --- a/libdrm/dri_bufmgr.h +++ b/libdrm/dri_bufmgr.h @@ -75,18 +75,7 @@ struct _dri_bufmgr { * to be used from the graphics device. */ dri_bo *(*bo_alloc)(dri_bufmgr *bufmgr_ctx, const char *name, - unsigned long size, unsigned int alignment, - uint64_t location_mask); - - /** - * Allocates a buffer object for a static allocation. - * - * Static allocations are ones such as the front buffer that are offered by - * the X Server, which are never evicted and never moved. - */ - dri_bo *(*bo_alloc_static)(dri_bufmgr *bufmgr_ctx, const char *name, - unsigned long offset, unsigned long size, - void *virtual, uint64_t location_mask); + unsigned long size, unsigned int alignment); /** Takes a reference on a buffer object */ void (*bo_reference)(dri_bo *bo); @@ -104,7 +93,7 @@ struct _dri_bufmgr { * buffer to complete, first. The resulting mapping is available at * buf->virtual. */ - int (*bo_map)(dri_bo *buf, GLboolean write_enable); + int (*bo_map)(dri_bo *buf, int write_enable); /** Reduces the refcount on the userspace mapping of the buffer object. */ int (*bo_unmap)(dri_bo *buf); @@ -140,31 +129,6 @@ struct _dri_bufmgr { */ void (*destroy)(dri_bufmgr *bufmgr); - /** - * Add relocation entry in reloc_buf, which will be updated with the - * target buffer's real offset on on command submission. - * - * Relocations remain in place for the lifetime of the buffer object. - * - * \param reloc_buf Buffer to write the relocation into. - * \param flags BO flags to be used in validating the target buffer. - * Applicable flags include: - * - DRM_BO_FLAG_READ: The buffer will be read in the process of - * command execution. - * - DRM_BO_FLAG_WRITE: The buffer will be written in the process of - * command execution. - * - DRM_BO_FLAG_MEM_TT: The buffer should be validated in TT memory. - * - DRM_BO_FLAG_MEM_VRAM: The buffer should be validated in video - * memory. - * \param delta Constant value to be added to the relocation target's offset. - * \param offset Byte offset within batch_buf of the relocated pointer. - * \param target Buffer whose offset should be written into the relocation - * entry. - */ - int (*emit_reloc)(dri_bo *reloc_buf, - uint32_t read_domains, uint32_t write_domain, - uint32_t delta, uint32_t offset, dri_bo *target); - /** * Processes the relocations, either in userland or by converting the list * for use in batchbuffer submission. @@ -183,17 +147,14 @@ struct _dri_bufmgr { void (*post_submit)(dri_bo *batch_buf); int (*check_aperture_space)(dri_bo *bo); - GLboolean debug; /**< Enables verbose debugging printouts */ + int debug; /**< Enables verbose debugging printouts */ }; dri_bo *dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size, - unsigned int alignment, uint64_t location_mask); -dri_bo *dri_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, - unsigned long offset, unsigned long size, - void *virtual, uint64_t location_mask); + unsigned int alignment); void dri_bo_reference(dri_bo *bo); void dri_bo_unreference(dri_bo *bo); -int dri_bo_map(dri_bo *buf, GLboolean write_enable); +int dri_bo_map(dri_bo *buf, int write_enable); int dri_bo_unmap(dri_bo *buf); int dri_bo_subdata(dri_bo *bo, unsigned long offset, @@ -202,12 +163,9 @@ int dri_bo_get_subdata(dri_bo *bo, unsigned long offset, unsigned long size, void *data); void dri_bo_wait_rendering(dri_bo *bo); -void dri_bufmgr_set_debug(dri_bufmgr *bufmgr, GLboolean enable_debug); +void dri_bufmgr_set_debug(dri_bufmgr *bufmgr, int enable_debug); void dri_bufmgr_destroy(dri_bufmgr *bufmgr); -int dri_emit_reloc(dri_bo *reloc_buf, - uint32_t read_domains, uint32_t write_domain, - uint32_t delta, uint32_t offset, dri_bo *target_buf); void *dri_process_relocs(dri_bo *batch_buf); void dri_post_process_relocs(dri_bo *batch_buf); void dri_post_submit(dri_bo *batch_buf); diff --git a/libdrm/intel/Makefile.am b/libdrm/intel/Makefile.am new file mode 100644 index 00000000..111204b1 --- /dev/null +++ b/libdrm/intel/Makefile.am @@ -0,0 +1,38 @@ +# 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 + +AM_CFLAGS = \ + $(WARN_CFLAGS) \ + -I$(top_srcdir)/shared-core + +noinst_LTLIBRARIES = libdrm_intel.la + +libdrm_intel_la_SOURCES = \ + intel_bufmgr_fake.c \ + intel_bufmgr_gem.c \ + mm.c \ + mm.h + +libdrm_intelincludedir = ${includedir} +libdrm_intelinclude_HEADERS = intel_bufmgr.h diff --git a/libdrm/intel/intel_bufmgr.h b/libdrm/intel/intel_bufmgr.h new file mode 100644 index 00000000..b9542eab --- /dev/null +++ b/libdrm/intel/intel_bufmgr.h @@ -0,0 +1,94 @@ +/* + * 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 intel_bufmgr.h + * + * Public definitions of Intel-specific bufmgr functions. + */ + +#ifndef INTEL_BUFMGR_GEM_H +#define INTEL_BUFMGR_GEM_H + +#include "dri_bufmgr.h" + +/** + * Intel-specific bufmgr bits that follow immediately after the + * generic bufmgr structure. + */ +struct intel_bufmgr { + /** + * Add relocation entry in reloc_buf, which will be updated with the + * target buffer's real offset on on command submission. + * + * Relocations remain in place for the lifetime of the buffer object. + * + * \param reloc_buf Buffer to write the relocation into. + * \param read_domains GEM read domains which the buffer will be read into + * by the command that this relocation is part of. + * \param write_domains GEM read domains which the buffer will be dirtied + * in by the command that this relocation is part of. + * \param delta Constant value to be added to the relocation target's + * offset. + * \param offset Byte offset within batch_buf of the relocated pointer. + * \param target Buffer whose offset should be written into the relocation + * entry. + */ + int (*emit_reloc)(dri_bo *reloc_buf, + uint32_t read_domains, uint32_t write_domain, + uint32_t delta, uint32_t offset, dri_bo *target); +}; + +/* intel_bufmgr_gem.c */ +dri_bufmgr *intel_bufmgr_gem_init(int fd, int batch_size); +dri_bo *intel_bo_gem_create_from_name(dri_bufmgr *bufmgr, const char *name, + unsigned int handle); +void intel_bufmgr_gem_enable_reuse(dri_bufmgr *bufmgr); + +/* intel_bufmgr_fake.c */ +dri_bufmgr *intel_bufmgr_fake_init(unsigned long low_offset, void *low_virtual, + unsigned long size, + unsigned int (*fence_emit)(void *private), + int (*fence_wait)(void *private, + unsigned int cookie), + void *driver_priv); +dri_bo *intel_bo_fake_alloc_static(dri_bufmgr *bufmgr, const char *name, + unsigned long offset, unsigned long size, + void *virtual); + +void intel_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr); +void intel_bo_fake_disable_backing_store(dri_bo *bo, + void (*invalidate_cb)(dri_bo *bo, + void *ptr), + void *ptr); + +int intel_bo_emit_reloc(dri_bo *reloc_buf, + uint32_t read_domains, uint32_t write_domain, + uint32_t delta, uint32_t offset, dri_bo *target_buf); + +#endif /* INTEL_BUFMGR_GEM_H */ + diff --git a/libdrm/intel/intel_bufmgr_fake.c b/libdrm/intel/intel_bufmgr_fake.c index 2aed3d85..1bddbeab 100644 --- a/libdrm/intel/intel_bufmgr_fake.c +++ b/libdrm/intel/intel_bufmgr_fake.c @@ -33,19 +33,19 @@ * programming interface, but is more expressive and avoids many of * the bugs in the old texture manager. */ -#include "mtypes.h" + +#include +#include +#include #include "dri_bufmgr.h" -#include "intel_bufmgr_fake.h" +#include "intel_bufmgr.h" #include "drm.h" #include "i915_drm.h" - -#include "simple_list.h" #include "mm.h" -#include "imports.h" #define DBG(...) do { \ if (bufmgr_fake->bufmgr.debug) \ - _mesa_printf(__VA_ARGS__); \ + drmMsg(__VA_ARGS__); \ } while (0) /* Internal flags: @@ -67,11 +67,11 @@ struct fake_buffer_reloc /** Buffer object that the relocation points at. */ dri_bo *target_buf; /** Offset of the relocation entry within reloc_buf. */ - GLuint offset; + uint32_t offset; /** Cached value of the offset when we last performed this relocation. */ - GLuint last_target_offset; + uint32_t last_target_offset; /** Value added to target_buf's offset to get the relocation entry. */ - GLuint delta; + uint32_t delta; /** Cache domains the target buffer is read into. */ uint32_t read_domains; /** Cache domain the target buffer will have dirty cachelines in. */ @@ -102,25 +102,36 @@ struct block { typedef struct _bufmgr_fake { dri_bufmgr bufmgr; + struct intel_bufmgr intel_bufmgr; unsigned long low_offset; unsigned long size; void *virtual; struct mem_block *heap; - struct block lru; /* only allocated, non-fence-pending blocks here */ unsigned buf_nr; /* for generating ids */ - struct block on_hardware; /* after bmValidateBuffers */ - struct block fenced; /* after bmFenceBuffers (mi_flush, emit irq, write dword) */ + /** + * List of blocks which are currently in the GART but haven't been + * fenced yet. + */ + struct block on_hardware; + /** + * List of blocks which are in the GART and have an active fence on them. + */ + struct block fenced; + /** + * List of blocks which have an expired fence and are ready to be evicted. + */ + struct block lru; /* then to bufmgr->lru or free() */ unsigned int last_fence; unsigned fail:1; unsigned need_fence:1; - GLboolean thrashing; + int thrashing; /** * Driver callback to emit a fence, returning the cookie. @@ -134,9 +145,9 @@ typedef struct _bufmgr_fake { /** Driver-supplied argument to driver callbacks */ void *driver_priv; - GLboolean debug; + int debug; - GLboolean performed_rendering; + int performed_rendering; /* keep track of the current total size of objects we have relocs for */ unsigned long current_total_size; @@ -163,12 +174,12 @@ typedef struct _dri_bo_fake { uint32_t write_domain; unsigned int alignment; - GLboolean is_static, validated; + int is_static, validated; unsigned int map_count; /** relocation list */ struct fake_buffer_reloc *relocs; - GLuint nr_relocs; + int nr_relocs; struct block *block; void *backing_store; @@ -183,18 +194,18 @@ static int dri_fake_check_aperture_space(dri_bo *bo); #define MAXFENCE 0x7fffffff -static GLboolean FENCE_LTE( unsigned a, unsigned b ) +static int FENCE_LTE( unsigned a, unsigned b ) { if (a == b) - return GL_TRUE; + return 1; if (a < b && b - a < (1<<24)) - return GL_TRUE; + return 1; if (a > b && MAXFENCE - a + b < (1<<24)) - return GL_TRUE; + return 1; - return GL_FALSE; + return 0; } static unsigned int @@ -211,14 +222,13 @@ _fence_wait_internal(dri_bufmgr_fake *bufmgr_fake, unsigned int cookie) ret = bufmgr_fake->fence_wait(bufmgr_fake->driver_priv, cookie); if (ret != 0) { - _mesa_printf("%s:%d: Error %d waiting for fence.\n", - __FILE__, __LINE__); + drmMsg("%s:%d: Error %d waiting for fence.\n", __FILE__, __LINE__); abort(); } clear_fenced(bufmgr_fake, cookie); } -static GLboolean +static int _fence_test(dri_bufmgr_fake *bufmgr_fake, unsigned fence) { /* Slight problem with wrap-around: @@ -229,39 +239,39 @@ _fence_test(dri_bufmgr_fake *bufmgr_fake, unsigned fence) /** * Allocate a memory manager block for the buffer. */ -static GLboolean +static int alloc_block(dri_bo *bo) { dri_bo_fake *bo_fake = (dri_bo_fake *)bo; dri_bufmgr_fake *bufmgr_fake= (dri_bufmgr_fake *)bo->bufmgr; struct block *block = (struct block *)calloc(sizeof *block, 1); - unsigned int align_log2 = _mesa_ffs(bo_fake->alignment) - 1; - GLuint sz; + unsigned int align_log2 = ffs(bo_fake->alignment) - 1; + unsigned int sz; if (!block) - return GL_FALSE; + return 1; sz = (bo->size + bo_fake->alignment - 1) & ~(bo_fake->alignment - 1); - block->mem = mmAllocMem(bufmgr_fake->heap, sz, align_log2, 0); + block->mem = drmmmAllocMem(bufmgr_fake->heap, sz, align_log2, 0); if (!block->mem) { free(block); - return GL_FALSE; + return 0; } - make_empty_list(block); + DRMINITLISTHEAD(block); /* Insert at head or at tail??? */ - insert_at_tail(&bufmgr_fake->lru, block); + DRMLISTADDTAIL(block, &bufmgr_fake->lru); - block->virtual = bufmgr_fake->virtual + + block->virtual = (uint8_t *)bufmgr_fake->virtual + block->mem->ofs - bufmgr_fake->low_offset; block->bo = bo; bo_fake->block = block; - return GL_TRUE; + return 1; } /* Release the card storage associated with buf: @@ -289,9 +299,9 @@ static void free_block(dri_bufmgr_fake *bufmgr_fake, struct block *block) } else { DBG(" - free immediately\n"); - remove_from_list(block); + DRMLISTDEL(block); - mmFreeMem(block->mem); + drmmmFreeMem(block->mem); free(block); } } @@ -304,7 +314,7 @@ alloc_backing_store(dri_bo *bo) assert(!bo_fake->backing_store); assert(!(bo_fake->flags & (BM_PINNED|BM_NO_BACKING_STORE))); - bo_fake->backing_store = ALIGN_MALLOC(bo->size, 64); + bo_fake->backing_store = malloc(bo->size); DBG("alloc_backing - buf %d %p %d\n", bo_fake->id, bo_fake->backing_store, bo->size); assert(bo_fake->backing_store); @@ -317,7 +327,7 @@ free_backing_store(dri_bo *bo) if (bo_fake->backing_store) { assert(!(bo_fake->flags & (BM_PINNED|BM_NO_BACKING_STORE))); - ALIGN_FREE(bo_fake->backing_store); + free(bo_fake->backing_store); bo_fake->backing_store = NULL; } } @@ -337,14 +347,14 @@ set_dirty(dri_bo *bo) bo_fake->dirty = 1; } -static GLboolean -evict_lru(dri_bufmgr_fake *bufmgr_fake, GLuint max_fence) +static int +evict_lru(dri_bufmgr_fake *bufmgr_fake, unsigned int max_fence) { struct block *block, *tmp; DBG("%s\n", __FUNCTION__); - foreach_s(block, tmp, &bufmgr_fake->lru) { + DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->lru) { dri_bo_fake *bo_fake = (dri_bo_fake *)block->bo; if (bo_fake != NULL && (bo_fake->flags & BM_NO_FENCE_SUBDATA)) @@ -357,23 +367,20 @@ evict_lru(dri_bufmgr_fake *bufmgr_fake, GLuint max_fence) bo_fake->block = NULL; free_block(bufmgr_fake, block); - return GL_TRUE; + return 1; } - return GL_FALSE; + return 0; } -#define foreach_s_rev(ptr, t, list) \ - for(ptr=(list)->prev,t=(ptr)->prev; list != ptr; ptr=t, t=(t)->prev) - -static GLboolean +static int evict_mru(dri_bufmgr_fake *bufmgr_fake) { struct block *block, *tmp; DBG("%s\n", __FUNCTION__); - foreach_s_rev(block, tmp, &bufmgr_fake->lru) { + DRMLISTFOREACHSAFEREVERSE(block, tmp, &bufmgr_fake->lru) { dri_bo_fake *bo_fake = (dri_bo_fake *)block->bo; if (bo_fake && (bo_fake->flags & BM_NO_FENCE_SUBDATA)) @@ -383,10 +390,10 @@ evict_mru(dri_bufmgr_fake *bufmgr_fake) bo_fake->block = NULL; free_block(bufmgr_fake, block); - return GL_TRUE; + return 1; } - return GL_FALSE; + return 0; } /** @@ -398,7 +405,7 @@ static int clear_fenced(dri_bufmgr_fake *bufmgr_fake, struct block *block, *tmp; int ret = 0; - foreach_s(block, tmp, &bufmgr_fake->fenced) { + DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->fenced) { assert(block->fenced); if (_fence_test(bufmgr_fake, block->fence)) { @@ -408,14 +415,15 @@ static int clear_fenced(dri_bufmgr_fake *bufmgr_fake, if (!block->bo) { DBG("delayed free: offset %x sz %x\n", block->mem->ofs, block->mem->size); - remove_from_list(block); - mmFreeMem(block->mem); + DRMLISTDEL(block); + drmmmFreeMem(block->mem); free(block); } else { DBG("return to lru: offset %x sz %x\n", block->mem->ofs, block->mem->size); - move_to_tail(&bufmgr_fake->lru, block); + DRMLISTDEL(block); + DRMLISTADDTAIL(block, &bufmgr_fake->lru); } ret = 1; @@ -438,7 +446,7 @@ static void fence_blocks(dri_bufmgr_fake *bufmgr_fake, unsigned fence) { struct block *block, *tmp; - foreach_s (block, tmp, &bufmgr_fake->on_hardware) { + DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->on_hardware) { DBG("Fence block %p (sz 0x%x ofs %x buf %p) with fence %d\n", block, block->mem->size, block->mem->ofs, block->bo, fence); block->fence = fence; @@ -448,13 +456,14 @@ static void fence_blocks(dri_bufmgr_fake *bufmgr_fake, unsigned fence) /* Move to tail of pending list here */ - move_to_tail(&bufmgr_fake->fenced, block); + DRMLISTDEL(block); + DRMLISTADDTAIL(block, &bufmgr_fake->fenced); } - assert(is_empty_list(&bufmgr_fake->on_hardware)); + assert(DRMLISTEMPTY(&bufmgr_fake->on_hardware)); } -static GLboolean evict_and_alloc_block(dri_bo *bo) +static int evict_and_alloc_block(dri_bo *bo) { dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; dri_bo_fake *bo_fake = (dri_bo_fake *)bo; @@ -464,7 +473,7 @@ static GLboolean evict_and_alloc_block(dri_bo *bo) /* Search for already free memory: */ if (alloc_block(bo)) - return GL_TRUE; + return 1; /* If we're not thrashing, allow lru eviction to dig deeper into * recently used textures. We'll probably be thrashing soon: @@ -472,7 +481,7 @@ static GLboolean evict_and_alloc_block(dri_bo *bo) if (!bufmgr_fake->thrashing) { while (evict_lru(bufmgr_fake, 0)) if (alloc_block(bo)) - return GL_TRUE; + return 1; } /* Keep thrashing counter alive? @@ -484,17 +493,17 @@ static GLboolean evict_and_alloc_block(dri_bo *bo) * freed memory that has been submitted to hardware and fenced to * become available: */ - while (!is_empty_list(&bufmgr_fake->fenced)) { - GLuint fence = bufmgr_fake->fenced.next->fence; + while (!DRMLISTEMPTY(&bufmgr_fake->fenced)) { + uint32_t fence = bufmgr_fake->fenced.next->fence; _fence_wait_internal(bufmgr_fake, fence); if (alloc_block(bo)) - return GL_TRUE; + return 1; } - if (!is_empty_list(&bufmgr_fake->on_hardware)) { - while (!is_empty_list(&bufmgr_fake->fenced)) { - GLuint fence = bufmgr_fake->fenced.next->fence; + if (!DRMLISTEMPTY(&bufmgr_fake->on_hardware)) { + while (!DRMLISTEMPTY(&bufmgr_fake->fenced)) { + uint32_t fence = bufmgr_fake->fenced.next->fence; _fence_wait_internal(bufmgr_fake, fence); } @@ -504,16 +513,16 @@ static GLboolean evict_and_alloc_block(dri_bo *bo) bufmgr_fake->thrashing = 20; if (alloc_block(bo)) - return GL_TRUE; + return 1; } while (evict_mru(bufmgr_fake)) if (alloc_block(bo)) - return GL_TRUE; + return 1; DBG("%s 0x%x bytes failed\n", __FUNCTION__, bo->size); - return GL_FALSE; + return 0; } /*********************************************************************** @@ -555,7 +564,7 @@ dri_fake_bo_wait_rendering(dri_bo *bo) * -- and wait for idle */ void -dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr) +intel_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr) { dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bufmgr; struct block *block, *tmp; @@ -572,10 +581,10 @@ dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr) /* Check that we hadn't released the lock without having fenced the last * set of buffers. */ - assert(is_empty_list(&bufmgr_fake->fenced)); - assert(is_empty_list(&bufmgr_fake->on_hardware)); + assert(DRMLISTEMPTY(&bufmgr_fake->fenced)); + assert(DRMLISTEMPTY(&bufmgr_fake->on_hardware)); - foreach_s(block, tmp, &bufmgr_fake->lru) { + DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->lru) { assert(_fence_test(bufmgr_fake, block->fence)); set_dirty(block->bo); } @@ -583,8 +592,7 @@ dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr) static dri_bo * dri_fake_bo_alloc(dri_bufmgr *bufmgr, const char *name, - unsigned long size, unsigned int alignment, - uint64_t location_mask) + unsigned long size, unsigned int alignment) { dri_bufmgr_fake *bufmgr_fake; dri_bo_fake *bo_fake; @@ -611,7 +619,7 @@ dri_fake_bo_alloc(dri_bufmgr *bufmgr, const char *name, bo_fake->id = ++bufmgr_fake->buf_nr; bo_fake->name = name; bo_fake->flags = 0; - bo_fake->is_static = GL_FALSE; + bo_fake->is_static = 0; DBG("drm_bo_alloc: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name, bo_fake->bo.size / 1024); @@ -619,10 +627,10 @@ dri_fake_bo_alloc(dri_bufmgr *bufmgr, const char *name, return &bo_fake->bo; } -static dri_bo * -dri_fake_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, - unsigned long offset, unsigned long size, - void *virtual, uint64_t location_mask) +dri_bo * +intel_bo_fake_alloc_static(dri_bufmgr *bufmgr, const char *name, + unsigned long offset, unsigned long size, + void *virtual) { dri_bufmgr_fake *bufmgr_fake; dri_bo_fake *bo_fake; @@ -643,7 +651,7 @@ dri_fake_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, bo_fake->id = ++bufmgr_fake->buf_nr; bo_fake->name = name; bo_fake->flags = BM_PINNED | DRM_BO_FLAG_NO_MOVE; - bo_fake->is_static = GL_TRUE; + bo_fake->is_static = 1; DBG("drm_bo_alloc_static: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name, bo_fake->bo.size / 1024); @@ -692,10 +700,10 @@ dri_fake_bo_unreference(dri_bo *bo) * Set the buffer as not requiring backing store, and instead get the callback * invoked whenever it would be set dirty. */ -void dri_bo_fake_disable_backing_store(dri_bo *bo, - void (*invalidate_cb)(dri_bo *bo, - void *ptr), - void *ptr) +void intel_bo_fake_disable_backing_store(dri_bo *bo, + void (*invalidate_cb)(dri_bo *bo, + void *ptr), + void *ptr) { dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; dri_bo_fake *bo_fake = (dri_bo_fake *)bo; @@ -723,7 +731,7 @@ void dri_bo_fake_disable_backing_store(dri_bo *bo, * BM_NO_BACKING_STORE or BM_PINNED) or backing store, as necessary. */ static int -dri_fake_bo_map(dri_bo *bo, GLboolean write_enable) +dri_fake_bo_map(dri_bo *bo, int write_enable) { dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; dri_bo_fake *bo_fake = (dri_bo_fake *)bo; @@ -744,7 +752,7 @@ dri_fake_bo_map(dri_bo *bo, GLboolean write_enable) bo_fake->bo.size / 1024); if (bo->virtual != NULL) { - _mesa_printf("%s: already mapped\n", __FUNCTION__); + drmMsg("%s: already mapped\n", __FUNCTION__); abort(); } else if (bo_fake->flags & (BM_NO_BACKING_STORE|BM_PINNED)) { @@ -807,16 +815,16 @@ dri_fake_kick_all(dri_bufmgr_fake *bufmgr_fake) { struct block *block, *tmp; - bufmgr_fake->performed_rendering = GL_FALSE; + bufmgr_fake->performed_rendering = 0; /* okay for ever BO that is on the HW kick it off. seriously not afraid of the POLICE right now */ - foreach_s(block, tmp, &bufmgr_fake->on_hardware) { + DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->on_hardware) { dri_bo_fake *bo_fake = (dri_bo_fake *)block->bo; block->on_hardware = 0; free_block(bufmgr_fake, block); bo_fake->block = NULL; - bo_fake->validated = GL_FALSE; + bo_fake->validated = 0; if (!(bo_fake->flags & BM_NO_BACKING_STORE)) bo_fake->dirty = 1; } @@ -890,9 +898,10 @@ dri_fake_bo_validate(dri_bo *bo) bo_fake->block->fenced = 0; bo_fake->block->on_hardware = 1; - move_to_tail(&bufmgr_fake->on_hardware, bo_fake->block); + DRMLISTDEL(bo_fake->block); + DRMLISTADDTAIL(bo_fake->block, &bufmgr_fake->on_hardware); - bo_fake->validated = GL_TRUE; + bo_fake->validated = 1; bufmgr_fake->need_fence = 1; return 0; @@ -915,7 +924,7 @@ dri_fake_destroy(dri_bufmgr *bufmgr) { dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bufmgr; - mmDestroy(bufmgr_fake->heap); + drmmmDestroy(bufmgr_fake->heap); free(bufmgr); } @@ -1018,9 +1027,9 @@ dri_fake_reloc_and_validate_buffer(dri_bo *bo) reloc_data = r->target_buf->offset + r->delta; if (bo->virtual == NULL) - dri_bo_map(bo, GL_TRUE); + dri_bo_map(bo, 1); - *(uint32_t *)(bo->virtual + r->offset) = reloc_data; + *(uint32_t *)((uint8_t *)bo->virtual + r->offset) = reloc_data; r->last_target_offset = r->target_buf->offset; } @@ -1036,7 +1045,7 @@ dri_fake_reloc_and_validate_buffer(dri_bo *bo) bo_fake->card_dirty = 1; } - bufmgr_fake->performed_rendering = GL_TRUE; + bufmgr_fake->performed_rendering = 1; } return dri_fake_bo_validate(bo); @@ -1050,7 +1059,7 @@ dri_fake_process_relocs(dri_bo *batch_buf) int ret; int retry_count = 0; - bufmgr_fake->performed_rendering = GL_FALSE; + bufmgr_fake->performed_rendering = 0; dri_fake_calculate_domains(batch_buf); @@ -1066,7 +1075,7 @@ dri_fake_process_relocs(dri_bo *batch_buf) bufmgr_fake->fail = 0; goto restart; } else /* dump out the memory here */ - mmDumpMemInfo(bufmgr_fake->heap); + drmmmDumpMemInfo(bufmgr_fake->heap); } assert(ret == 0); @@ -1095,7 +1104,7 @@ dri_bo_fake_post_submit(dri_bo *bo) } assert(bo_fake->map_count == 0); - bo_fake->validated = GL_FALSE; + bo_fake->validated = 0; bo_fake->read_domains = 0; bo_fake->write_domain = 0; } @@ -1114,7 +1123,7 @@ dri_fake_check_aperture_space(dri_bo *bo) { dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bo->bufmgr; dri_bo_fake *bo_fake = (dri_bo_fake *)bo; - GLuint sz; + unsigned int sz; sz = (bo->size + bo_fake->alignment - 1) & ~(bo_fake->alignment - 1); @@ -1133,40 +1142,39 @@ dri_fake_check_aperture_space(dri_bo *bo) } dri_bufmgr * -dri_bufmgr_fake_init(unsigned long low_offset, void *low_virtual, - unsigned long size, - unsigned int (*fence_emit)(void *private), - int (*fence_wait)(void *private, unsigned int cookie), - void *driver_priv) +intel_bufmgr_fake_init(unsigned long low_offset, void *low_virtual, + unsigned long size, + unsigned int (*fence_emit)(void *private), + int (*fence_wait)(void *private, unsigned int cookie), + void *driver_priv) { dri_bufmgr_fake *bufmgr_fake; bufmgr_fake = calloc(1, sizeof(*bufmgr_fake)); /* Initialize allocator */ - make_empty_list(&bufmgr_fake->fenced); - make_empty_list(&bufmgr_fake->on_hardware); - make_empty_list(&bufmgr_fake->lru); + DRMINITLISTHEAD(&bufmgr_fake->fenced); + DRMINITLISTHEAD(&bufmgr_fake->on_hardware); + DRMINITLISTHEAD(&bufmgr_fake->lru); bufmgr_fake->low_offset = low_offset; bufmgr_fake->virtual = low_virtual; bufmgr_fake->size = size; - bufmgr_fake->heap = mmInit(low_offset, size); + bufmgr_fake->heap = drmmmInit(low_offset, size); /* Hook in methods */ bufmgr_fake->bufmgr.bo_alloc = dri_fake_bo_alloc; - bufmgr_fake->bufmgr.bo_alloc_static = dri_fake_bo_alloc_static; bufmgr_fake->bufmgr.bo_reference = dri_fake_bo_reference; bufmgr_fake->bufmgr.bo_unreference = dri_fake_bo_unreference; bufmgr_fake->bufmgr.bo_map = dri_fake_bo_map; bufmgr_fake->bufmgr.bo_unmap = dri_fake_bo_unmap; bufmgr_fake->bufmgr.bo_wait_rendering = dri_fake_bo_wait_rendering; bufmgr_fake->bufmgr.destroy = dri_fake_destroy; - bufmgr_fake->bufmgr.emit_reloc = dri_fake_emit_reloc; bufmgr_fake->bufmgr.process_relocs = dri_fake_process_relocs; bufmgr_fake->bufmgr.post_submit = dri_fake_post_submit; bufmgr_fake->bufmgr.check_aperture_space = dri_fake_check_aperture_space; - bufmgr_fake->bufmgr.debug = GL_FALSE; + bufmgr_fake->bufmgr.debug = 0; + bufmgr_fake->intel_bufmgr.emit_reloc = dri_fake_emit_reloc; bufmgr_fake->fence_emit = fence_emit; bufmgr_fake->fence_wait = fence_wait; diff --git a/libdrm/intel/intel_bufmgr_fake.h b/libdrm/intel/intel_bufmgr_fake.h deleted file mode 100644 index bc7e59e6..00000000 --- a/libdrm/intel/intel_bufmgr_fake.h +++ /dev/null @@ -1,50 +0,0 @@ -/************************************************************************** - * - * Copyright © 2007 Intel Corporation - * 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 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. - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * - **************************************************************************/ -/* - * Authors: Thomas Hellström - * Keith Whitwell - * Eric Anholt - */ - -#ifndef _INTEL_BUFMGR_FAKE_H_ -#define _INTEL_BUFMGR_FAKE_H_ - -void dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr); -dri_bufmgr *dri_bufmgr_fake_init(unsigned long low_offset, void *low_virtual, - unsigned long size, - unsigned int (*fence_emit)(void *private), - int (*fence_wait)(void *private, - unsigned int cookie), - void *driver_priv); -void dri_bo_fake_disable_backing_store(dri_bo *bo, - void (*invalidate_cb)(dri_bo *bo, - void *ptr), - void *ptr); -#endif /* _INTEL_BUFMGR_FAKE_H_ */ - diff --git a/libdrm/intel/intel_bufmgr_gem.c b/libdrm/intel/intel_bufmgr_gem.c index 3c1c3157..20f39b5f 100644 --- a/libdrm/intel/intel_bufmgr_gem.c +++ b/libdrm/intel/intel_bufmgr_gem.c @@ -44,15 +44,12 @@ #include #include "errno.h" -#include "mtypes.h" #include "dri_bufmgr.h" +#include "intel_bufmgr.h" #include "string.h" -#include "imports.h" #include "i915_drm.h" -#include "intel_bufmgr_gem.h" - #define DBG(...) do { \ if (bufmgr_gem->bufmgr.debug) \ fprintf(stderr, __VA_ARGS__); \ @@ -89,9 +86,11 @@ struct dri_gem_bo_bucket { typedef struct _dri_bufmgr_gem { dri_bufmgr bufmgr; + struct intel_bufmgr intel_bufmgr; + int fd; - uint32_t max_relocs; + int max_relocs; struct drm_i915_gem_exec_object *exec_objects; dri_bo **exec_bos; @@ -108,7 +107,8 @@ typedef struct _dri_bo_gem { dri_bo bo; int refcount; - GLboolean mapped; + /** Boolean whether the mmap ioctl has been called for this buffer yet. */ + int mapped; uint32_t gem_handle; const char *name; @@ -119,11 +119,11 @@ typedef struct _dri_bo_gem { int validate_index; /** - * Tracks whether set_domain to CPU is current + * Boolean whether set_domain to CPU is current * Set when set_domain has been called * Cleared when a batch has been submitted */ - GLboolean cpu_domain_set; + int cpu_domain_set; /** Array passed to the DRM containing relocation information. */ struct drm_i915_gem_relocation_entry *relocs; @@ -138,8 +138,8 @@ typedef struct _dri_bo_gem { static int logbase2(int n) { - GLint i = 1; - GLint log2 = 0; + int i = 1; + int log2 = 0; while (n > i) { i *= 2; @@ -262,15 +262,14 @@ intel_setup_reloc_list(dri_bo *bo) static dri_bo * dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, - unsigned long size, unsigned int alignment, - uint64_t location_mask) + unsigned long size, unsigned int alignment) { dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr; dri_bo_gem *bo_gem; unsigned int page_size = getpagesize(); int ret; struct dri_gem_bo_bucket *bucket; - GLboolean alloc_from_cache = GL_FALSE; + int alloc_from_cache = 0; bo_gem = calloc(1, sizeof(*bo_gem)); if (!bo_gem) @@ -338,18 +337,6 @@ dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, return &bo_gem->bo; } -/* Our GEM backend doesn't allow creation of static buffers, as that requires - * privelege for the non-fake case, and the lock in the fake case where we were - * working around the X Server not creating buffers and passing handles to us. - */ -static dri_bo * -dri_gem_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, - unsigned long offset, unsigned long size, void *virtual, - uint64_t location_mask) -{ - return NULL; -} - /** * Returns a dri_bo wrapping the given buffer object handle. * @@ -357,7 +344,7 @@ dri_gem_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, * to another. */ dri_bo * -intel_gem_bo_create_from_handle(dri_bufmgr *bufmgr, const char *name, +intel_bo_gem_create_from_name(dri_bufmgr *bufmgr, const char *name, unsigned int handle) { dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr; @@ -465,7 +452,7 @@ dri_gem_bo_unreference(dri_bo *bo) } static int -dri_gem_bo_map(dri_bo *bo, GLboolean write_enable) +dri_gem_bo_map(dri_bo *bo, int write_enable) { dri_bufmgr_gem *bufmgr_gem; dri_bo_gem *bo_gem = (dri_bo_gem *)bo; @@ -499,7 +486,7 @@ dri_gem_bo_map(dri_bo *bo, GLboolean write_enable) bo_gem->virtual = (void *)(uintptr_t)mmap_arg.addr_ptr; } bo->virtual = bo_gem->virtual; - bo_gem->mapped = GL_TRUE; + bo_gem->mapped = 1; DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name, bo_gem->virtual); } @@ -514,7 +501,7 @@ dri_gem_bo_map(dri_bo *bo, GLboolean write_enable) bo_gem->gem_handle, set_domain.read_domains, set_domain.write_domain, strerror (errno)); } - bo_gem->cpu_domain_set = GL_TRUE; + bo_gem->cpu_domain_set = 1; } return 0; @@ -763,7 +750,7 @@ dri_gem_post_submit(dri_bo *batch_buf) dri_bo_gem *bo_gem = (dri_bo_gem *)bo; /* Need to call set_domain on next bo_map */ - bo_gem->cpu_domain_set = GL_FALSE; + bo_gem->cpu_domain_set = 0; /* Disconnect the buffer from the validate list */ bo_gem->validate_index = -1; @@ -781,7 +768,7 @@ dri_gem_post_submit(dri_bo *batch_buf) * in flight at once. */ void -intel_gem_enable_bo_reuse(dri_bufmgr *bufmgr) +intel_bufmgr_gem_enable_reuse(dri_bufmgr *bufmgr) { dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr; int i; @@ -824,7 +811,6 @@ intel_bufmgr_gem_init(int fd, int batch_size) bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2; bufmgr_gem->bufmgr.bo_alloc = dri_gem_bo_alloc; - bufmgr_gem->bufmgr.bo_alloc_static = dri_gem_bo_alloc_static; bufmgr_gem->bufmgr.bo_reference = dri_gem_bo_reference; bufmgr_gem->bufmgr.bo_unreference = dri_gem_bo_unreference; bufmgr_gem->bufmgr.bo_map = dri_gem_bo_map; @@ -833,11 +819,11 @@ intel_bufmgr_gem_init(int fd, int batch_size) bufmgr_gem->bufmgr.bo_get_subdata = dri_gem_bo_get_subdata; bufmgr_gem->bufmgr.bo_wait_rendering = dri_gem_bo_wait_rendering; bufmgr_gem->bufmgr.destroy = dri_bufmgr_gem_destroy; - bufmgr_gem->bufmgr.emit_reloc = dri_gem_emit_reloc; bufmgr_gem->bufmgr.process_relocs = dri_gem_process_reloc; bufmgr_gem->bufmgr.post_submit = dri_gem_post_submit; - bufmgr_gem->bufmgr.debug = GL_FALSE; + bufmgr_gem->bufmgr.debug = 0; bufmgr_gem->bufmgr.check_aperture_space = dri_gem_check_aperture_space; + bufmgr_gem->intel_bufmgr.emit_reloc = dri_gem_emit_reloc; /* Initialize the linked lists for BO reuse cache. */ for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) bufmgr_gem->cache_bucket[i].tail = &bufmgr_gem->cache_bucket[i].head; @@ -845,3 +831,15 @@ intel_bufmgr_gem_init(int fd, int batch_size) return &bufmgr_gem->bufmgr; } +int +intel_bo_emit_reloc(dri_bo *reloc_buf, + uint32_t read_domains, uint32_t write_domain, + uint32_t delta, uint32_t offset, dri_bo *target_buf) +{ + struct intel_bufmgr *intel_bufmgr; + + intel_bufmgr = (struct intel_bufmgr *)(reloc_buf->bufmgr + 1); + + return intel_bufmgr->emit_reloc(reloc_buf, read_domains, write_domain, + delta, offset, target_buf); +} diff --git a/libdrm/intel/intel_bufmgr_gem.h b/libdrm/intel/intel_bufmgr_gem.h deleted file mode 100644 index 36caeba2..00000000 --- a/libdrm/intel/intel_bufmgr_gem.h +++ /dev/null @@ -1,16 +0,0 @@ - -#ifndef INTEL_BUFMGR_GEM_H -#define INTEL_BUFMGR_GEM_H - -#include "dri_bufmgr.h" - -extern dri_bo *intel_gem_bo_create_from_handle(dri_bufmgr *bufmgr, - const char *name, - unsigned int handle); - -dri_bufmgr *intel_bufmgr_gem_init(int fd, int batch_size); - -void -intel_gem_enable_bo_reuse(dri_bufmgr *bufmgr); - -#endif /* INTEL_BUFMGR_GEM_H */ diff --git a/libdrm/intel/mm.c b/libdrm/intel/mm.c new file mode 100644 index 00000000..2605d8ec --- /dev/null +++ b/libdrm/intel/mm.c @@ -0,0 +1,281 @@ +/* + * GLX Hardware Device Driver common code + * Copyright (C) 1999 Wittawat Yamwong + * + * 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 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 + * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS 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. + * + */ + +#include +#include + +#include "xf86drm.h" +#include "mm.h" + +void +drmmmDumpMemInfo(const struct mem_block *heap) +{ + drmMsg("Memory heap %p:\n", (void *)heap); + if (heap == 0) { + drmMsg(" heap == 0\n"); + } else { + const struct mem_block *p; + + for(p = heap->next; p != heap; p = p->next) { + drmMsg(" Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size, + p->free ? 'F':'.', + p->reserved ? 'R':'.'); + } + + drmMsg("\nFree list:\n"); + + for(p = heap->next_free; p != heap; p = p->next_free) { + drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size, + p->free ? 'F':'.', + p->reserved ? 'R':'.'); + } + + } + drmMsg("End of memory blocks\n"); +} + +struct mem_block * +drmmmInit(int ofs, int size) +{ + struct mem_block *heap, *block; + + if (size <= 0) + return NULL; + + heap = (struct mem_block *) calloc(1, sizeof(struct mem_block)); + if (!heap) + return NULL; + + block = (struct mem_block *) calloc(1, sizeof(struct mem_block)); + if (!block) { + free(heap); + return NULL; + } + + heap->next = block; + heap->prev = block; + heap->next_free = block; + heap->prev_free = block; + + block->heap = heap; + block->next = heap; + block->prev = heap; + block->next_free = heap; + block->prev_free = heap; + + block->ofs = ofs; + block->size = size; + block->free = 1; + + return heap; +} + + +static struct mem_block * +SliceBlock(struct mem_block *p, + int startofs, int size, + int reserved, int alignment) +{ + struct mem_block *newblock; + + /* break left [p, newblock, p->next], then p = newblock */ + if (startofs > p->ofs) { + newblock = (struct mem_block*) calloc(1, sizeof(struct mem_block)); + if (!newblock) + return NULL; + newblock->ofs = startofs; + newblock->size = p->size - (startofs - p->ofs); + newblock->free = 1; + newblock->heap = p->heap; + + newblock->next = p->next; + newblock->prev = p; + p->next->prev = newblock; + p->next = newblock; + + newblock->next_free = p->next_free; + newblock->prev_free = p; + p->next_free->prev_free = newblock; + p->next_free = newblock; + + p->size -= newblock->size; + p = newblock; + } + + /* break right, also [p, newblock, p->next] */ + if (size < p->size) { + newblock = (struct mem_block*) calloc(1, sizeof(struct mem_block)); + if (!newblock) + return NULL; + newblock->ofs = startofs + size; + newblock->size = p->size - size; + newblock->free = 1; + newblock->heap = p->heap; + + newblock->next = p->next; + newblock->prev = p; + p->next->prev = newblock; + p->next = newblock; + + newblock->next_free = p->next_free; + newblock->prev_free = p; + p->next_free->prev_free = newblock; + p->next_free = newblock; + + p->size = size; + } + + /* p = middle block */ + p->free = 0; + + /* Remove p from the free list: + */ + p->next_free->prev_free = p->prev_free; + p->prev_free->next_free = p->next_free; + + p->next_free = 0; + p->prev_free = 0; + + p->reserved = reserved; + return p; +} + + +struct mem_block * +drmmmAllocMem(struct mem_block *heap, int size, int align2, int startSearch) +{ + struct mem_block *p; + const int mask = (1 << align2)-1; + int startofs = 0; + int endofs; + + if (!heap || align2 < 0 || size <= 0) + return NULL; + + for (p = heap->next_free; p != heap; p = p->next_free) { + assert(p->free); + + startofs = (p->ofs + mask) & ~mask; + if ( startofs < startSearch ) { + startofs = startSearch; + } + endofs = startofs+size; + if (endofs <= (p->ofs+p->size)) + break; + } + + if (p == heap) + return NULL; + + assert(p->free); + p = SliceBlock(p,startofs,size,0,mask+1); + + return p; +} + + +struct mem_block * +drmmmFindBlock(struct mem_block *heap, int start) +{ + struct mem_block *p; + + for (p = heap->next; p != heap; p = p->next) { + if (p->ofs == start) + return p; + } + + return NULL; +} + + +static int +Join2Blocks(struct mem_block *p) +{ + /* XXX there should be some assertions here */ + + /* NOTE: heap->free == 0 */ + + if (p->free && p->next->free) { + struct mem_block *q = p->next; + + assert(p->ofs + p->size == q->ofs); + p->size += q->size; + + p->next = q->next; + q->next->prev = p; + + q->next_free->prev_free = q->prev_free; + q->prev_free->next_free = q->next_free; + + free(q); + return 1; + } + return 0; +} + +int +drmmmFreeMem(struct mem_block *b) +{ + if (!b) + return 0; + + if (b->free) { + drmMsg("block already free\n"); + return -1; + } + if (b->reserved) { + drmMsg("block is reserved\n"); + return -1; + } + + b->free = 1; + b->next_free = b->heap->next_free; + b->prev_free = b->heap; + b->next_free->prev_free = b; + b->prev_free->next_free = b; + + Join2Blocks(b); + if (b->prev != b->heap) + Join2Blocks(b->prev); + + return 0; +} + + +void +drmmmDestroy(struct mem_block *heap) +{ + struct mem_block *p; + + if (!heap) + return; + + for (p = heap->next; p != heap; ) { + struct mem_block *next = p->next; + free(p); + p = next; + } + + free(heap); +} diff --git a/libdrm/intel/mm.h b/libdrm/intel/mm.h new file mode 100644 index 00000000..965bb0cd --- /dev/null +++ b/libdrm/intel/mm.h @@ -0,0 +1,88 @@ +/* + * GLX Hardware Device Driver common code + * Copyright (C) 1999 Wittawat Yamwong + * + * 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 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 + * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS 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. + */ + + +/** + * Memory manager code. Primarily used by device drivers to manage texture + * heaps, etc. + */ + + +#ifndef MM_H +#define MM_H + +struct mem_block { + struct mem_block *next, *prev; + struct mem_block *next_free, *prev_free; + struct mem_block *heap; + int ofs,size; + unsigned int free:1; + unsigned int reserved:1; +}; + + + +/** + * input: total size in bytes + * return: a heap pointer if OK, NULL if error + */ +extern struct mem_block *drmmmInit(int ofs, int size); + +/** + * Allocate 'size' bytes with 2^align2 bytes alignment, + * restrict the search to free memory after 'startSearch' + * depth and back buffers should be in different 4mb banks + * to get better page hits if possible + * input: size = size of block + * align2 = 2^align2 bytes alignment + * startSearch = linear offset from start of heap to begin search + * return: pointer to the allocated block, 0 if error + */ +extern struct mem_block *drmmmAllocMem(struct mem_block *heap, int size, + int align2, int startSearch); + +/** + * Free block starts at offset + * input: pointer to a block + * return: 0 if OK, -1 if error + */ +extern int drmmmFreeMem(struct mem_block *b); + +/** + * Free block starts at offset + * input: pointer to a heap, start offset + * return: pointer to a block + */ +extern struct mem_block *drmmmFindBlock(struct mem_block *heap, int start); + +/** + * destroy MM + */ +extern void drmmmDestroy(struct mem_block *mmInit); + +/** + * For debuging purpose. + */ +extern void drmmmDumpMemInfo(const struct mem_block *mmInit); + +#endif diff --git a/libdrm/xf86drm.c b/libdrm/xf86drm.c index 28f481ff..0383b6b8 100644 --- a/libdrm/xf86drm.c +++ b/libdrm/xf86drm.c @@ -110,7 +110,7 @@ static int drmDebugPrint(const char *format, va_list ap) static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint; -static void +void drmMsg(const char *format, ...) { va_list ap; diff --git a/libdrm/xf86drm.h b/libdrm/xf86drm.h index 230f54ce..6ced284a 100644 --- a/libdrm/xf86drm.h +++ b/libdrm/xf86drm.h @@ -657,6 +657,7 @@ extern int drmSLLookupNeighbors(void *l, unsigned long key, extern int drmOpenOnce(void *unused, const char *BusID, int *newlyopened); extern void drmCloseOnce(int fd); +extern void drmMsg(const char *format, ...); #include "xf86mm.h" diff --git a/libdrm/xf86mm.h b/libdrm/xf86mm.h index bb573407..a31de424 100644 --- a/libdrm/xf86mm.h +++ b/libdrm/xf86mm.h @@ -94,6 +94,18 @@ typedef struct _drmMMListHead #define DRMLISTENTRY(__type, __item, __field) \ ((__type *)(((char *) (__item)) - offsetof(__type, __field))) +#define DRMLISTEMPTY(__item) ((__item)->next == (__item)) + +#define DRMLISTFOREACHSAFE(__item, __temp, __list) \ + for ((__item) = (__list)->next, (__temp) = (__item)->next; \ + (__item) != (__list); \ + (__item) = (__temp), (__temp) = (__item)->next) + +#define DRMLISTFOREACHSAFEREVERSE(__item, __temp, __list) \ + for ((__item) = (__list)->prev, (__temp) = (__item)->prev; \ + (__item) != (__list); \ + (__item) = (__temp), (__temp) = (__item)->prev) + typedef struct _drmFence { unsigned handle; -- cgit v1.2.3 From 8a3b510405f9fc6c920c456e3481bddd296a2af7 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 3 Jun 2008 14:34:54 -0700 Subject: Fix libdrm to actually include the new code instead of just building it. --- libdrm/Makefile.am | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libdrm/Makefile.am b/libdrm/Makefile.am index c8530286..1187517a 100644 --- a/libdrm/Makefile.am +++ b/libdrm/Makefile.am @@ -25,8 +25,9 @@ libdrm_ladir = $(libdir) libdrm_la_LDFLAGS = -version-number 2:3:0 -no-undefined AM_CFLAGS = -I$(top_srcdir)/shared-core -libdrm_la_SOURCES = xf86drm.c xf86drmHash.c xf86drmRandom.c xf86drmSL.c -libdrm_la_DEPENDENCIES = intel/libdrm_intel.la +libdrm_la_SOURCES = xf86drm.c xf86drmHash.c xf86drmRandom.c xf86drmSL.c \ + dri_bufmgr.c +libdrm_la_LIBADD = intel/libdrm_intel.la libdrmincludedir = ${includedir} libdrminclude_HEADERS = xf86drm.h xf86mm.h dri_bufmgr.h -- 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(-) 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 d198e9b091e919a90895e551e48fb30d36a849ef Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 5 Jun 2008 08:44:46 -0700 Subject: Add a function to bufmgr_fake to evict all buffers in the GTT. This will be used by the X Server for VT switch. --- libdrm/intel/intel_bufmgr.h | 1 + libdrm/intel/intel_bufmgr_fake.c | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/libdrm/intel/intel_bufmgr.h b/libdrm/intel/intel_bufmgr.h index b9542eab..1cf0d518 100644 --- a/libdrm/intel/intel_bufmgr.h +++ b/libdrm/intel/intel_bufmgr.h @@ -85,6 +85,7 @@ void intel_bo_fake_disable_backing_store(dri_bo *bo, void (*invalidate_cb)(dri_bo *bo, void *ptr), void *ptr); +void intel_bufmgr_fake_evict_all(dri_bufmgr *bufmgr); int intel_bo_emit_reloc(dri_bo *reloc_buf, uint32_t read_domains, uint32_t write_domain, diff --git a/libdrm/intel/intel_bufmgr_fake.c b/libdrm/intel/intel_bufmgr_fake.c index 1bddbeab..3f5a22d3 100644 --- a/libdrm/intel/intel_bufmgr_fake.c +++ b/libdrm/intel/intel_bufmgr_fake.c @@ -125,7 +125,6 @@ typedef struct _bufmgr_fake { * List of blocks which have an expired fence and are ready to be evicted. */ struct block lru; - /* then to bufmgr->lru or free() */ unsigned int last_fence; @@ -1141,6 +1140,40 @@ dri_fake_check_aperture_space(dri_bo *bo) return 0; } +/** + * Evicts all buffers, waiting for fences to pass and copying contents out + * as necessary. + * + * Used by the X Server on LeaveVT, when the card memory is no longer our + * own. + */ +void +intel_bufmgr_fake_evict_all(dri_bufmgr *bufmgr) +{ + dri_bufmgr_fake *bufmgr_fake = (dri_bufmgr_fake *)bufmgr; + struct block *block, *tmp; + + bufmgr_fake->need_fence = 1; + bufmgr_fake->fail = 0; + + /* Wait for hardware idle. We don't know where acceleration has been + * happening, so we'll need to wait anyway before letting anything get + * put on the card again. + */ + dri_bufmgr_fake_wait_idle(bufmgr_fake); + + /* Check that we hadn't released the lock without having fenced the last + * set of buffers. + */ + assert(DRMLISTEMPTY(&bufmgr_fake->fenced)); + assert(DRMLISTEMPTY(&bufmgr_fake->on_hardware)); + + DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->lru) { + /* Releases the memory, and memcpys dirty contents out if necessary. */ + free_block(bufmgr_fake, block); + } +} + dri_bufmgr * intel_bufmgr_fake_init(unsigned long low_offset, void *low_virtual, unsigned long size, -- 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(+) 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 84162ccb7dc0286336292ac7f8e80678bfc11804 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 5 Jun 2008 13:49:21 -0700 Subject: Ignore X server provided mmio address --- shared-core/i915_dma.c | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index a948834a..37c822cf 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -214,14 +214,6 @@ static int i915_initialize(struct drm_device * dev, return -EINVAL; } - if (init->mmio_offset != 0) - dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset); - if (!dev_priv->mmio_map) { - i915_dma_cleanup(dev); - DRM_ERROR("can not find mmio map!\n"); - return -EINVAL; - } - #ifdef I915_HAVE_BUFFER dev_priv->max_validate_buffers = I915_MAX_VALIDATE_BUFFERS; #endif @@ -323,11 +315,6 @@ static int i915_dma_resume(struct drm_device * dev) return -EINVAL; } - if (!dev_priv->mmio_map) { - DRM_ERROR("can not find mmio map!\n"); - return -EINVAL; - } - if (dev_priv->ring.map.handle == NULL) { DRM_ERROR("can not ioremap virtual address for" " ring buffer\n"); -- cgit v1.2.3 From 5f5badb26f761eec87b951ce1b7b3a51a5060c50 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 5 Jun 2008 14:09:57 -0700 Subject: [intel] Allocate hardware status page at driver load time I couldn't get the re-allocated HWS to work on my 965GM, so I just gave up and made it persist across the lifetime of the driver instead. --- shared-core/i915_dma.c | 80 +++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 37c822cf..1ea5c28e 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -69,6 +69,44 @@ int i915_wait_ring(struct drm_device * dev, int n, const char *caller) return -EBUSY; } +int i915_init_hardware_status(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + /* Program Hardware Status Page */ + dev_priv->status_page_dmah = + drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff); + + if (!dev_priv->status_page_dmah) { + DRM_ERROR("Can not allocate hardware status page\n"); + return -ENOMEM; + } + dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr; + dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr; + + memset(dev_priv->hw_status_page, 0, PAGE_SIZE); + + I915_WRITE(0x02080, dev_priv->dma_status_page); + DRM_DEBUG("Enabled hardware status page\n"); + return 0; +} + +void i915_free_hardware_status(struct drm_device *dev) +{ + drm_i915_private_t *dev_priv = dev->dev_private; + if (dev_priv->status_page_dmah) { + drm_pci_free(dev, dev_priv->status_page_dmah); + dev_priv->status_page_dmah = NULL; + /* Need to rewrite hardware status page */ + I915_WRITE(0x02080, 0x1ffff000); + } + + if (dev_priv->status_gfx_addr) { + dev_priv->status_gfx_addr = 0; + drm_core_ioremapfree(&dev_priv->hws_map, dev); + I915_WRITE(0x02080, 0x1ffff000); + } +} + #if I915_RING_VALIDATE /** * Validate the cached ring tail value @@ -122,18 +160,8 @@ static int i915_dma_cleanup(struct drm_device * dev) dev_priv->ring.map.size = 0; } - if (dev_priv->status_page_dmah) { - drm_pci_free(dev, dev_priv->status_page_dmah); - dev_priv->status_page_dmah = NULL; - /* Need to rewrite hardware status page */ - I915_WRITE(0x02080, 0x1ffff000); - } - - if (dev_priv->status_gfx_addr) { - dev_priv->status_gfx_addr = 0; - drm_core_ioremapfree(&dev_priv->hws_map, dev); - I915_WRITE(0x02080, 0x1ffff000); - } + if (I915_NEED_GFX_HWS(dev)) + i915_free_hardware_status(dev); return 0; } @@ -269,24 +297,6 @@ static int i915_initialize(struct drm_device * dev, */ dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A; - /* Program Hardware Status Page */ - if (!I915_NEED_GFX_HWS(dev)) { - dev_priv->status_page_dmah = - drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff); - - if (!dev_priv->status_page_dmah) { - i915_dma_cleanup(dev); - DRM_ERROR("Can not allocate hardware status page\n"); - return -ENOMEM; - } - dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr; - dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr; - - memset(dev_priv->hw_status_page, 0, PAGE_SIZE); - - I915_WRITE(0x02080, dev_priv->dma_status_page); - } - DRM_DEBUG("Enabled hardware status page\n"); #ifdef I915_HAVE_BUFFER mutex_init(&dev_priv->cmdbuf_mutex); #endif @@ -1078,6 +1088,14 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags) #endif #endif + /* Init HWS + */ + if (!I915_NEED_GFX_HWS(dev)) { + ret = i915_init_hardware_status(dev); + if(ret) + return ret; + } + return ret; } @@ -1095,6 +1113,8 @@ int i915_driver_unload(struct drm_device *dev) intel_fini_chipset_flush_compat(dev); #endif #endif + i915_free_hardware_status(dev); + return 0; } -- cgit v1.2.3 From a919ff5d5ec2fe716cbf5c593be7cc0705499107 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 5 Jun 2008 15:58:09 -0700 Subject: [libdrm/intel] Reuse entire dri_bo_gem structure The code was discarding the dri_bo_gem structure and saving only the kernel handle. This lost the mmap address, causing pain when the next buffer user wanted to map the buffer. --- libdrm/intel/intel_bufmgr_gem.c | 76 +++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/libdrm/intel/intel_bufmgr_gem.c b/libdrm/intel/intel_bufmgr_gem.c index 20f39b5f..08cb5d6b 100644 --- a/libdrm/intel/intel_bufmgr_gem.c +++ b/libdrm/intel/intel_bufmgr_gem.c @@ -55,15 +55,16 @@ fprintf(stderr, __VA_ARGS__); \ } while (0) +typedef struct _dri_bo_gem dri_bo_gem; + struct intel_validate_entry { - dri_bo *bo; + dri_bo_gem *bo_gem; struct drm_i915_op_arg bo_arg; }; struct dri_gem_bo_bucket_entry { - uint32_t gem_handle; - uint32_t last_offset; - struct dri_gem_bo_bucket_entry *next; + dri_bo_gem *bo_gem; + struct dri_gem_bo_bucket_entry *next; }; struct dri_gem_bo_bucket { @@ -103,7 +104,7 @@ typedef struct _dri_bufmgr_gem { struct drm_i915_gem_execbuffer exec_arg; } dri_bufmgr_gem; -typedef struct _dri_bo_gem { +struct _dri_bo_gem { dri_bo bo; int refcount; @@ -133,7 +134,7 @@ typedef struct _dri_bo_gem { int reloc_count; /** Mapped address for the buffer */ void *virtual; -} dri_bo_gem; +}; static int logbase2(int n) @@ -270,24 +271,21 @@ dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, int ret; struct dri_gem_bo_bucket *bucket; int alloc_from_cache = 0; - - bo_gem = calloc(1, sizeof(*bo_gem)); - if (!bo_gem) - return NULL; + unsigned long bo_size; /* Round the allocated size up to a power of two number of pages. */ - bo_gem->bo.size = 1 << logbase2(size); - if (bo_gem->bo.size < page_size) - bo_gem->bo.size = page_size; - bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo_gem->bo.size); + bo_size = 1 << logbase2(size); + if (bo_size < page_size) + bo_size = page_size; + bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo_size); /* If we don't have caching at this size, don't actually round the * allocation up. */ if (bucket == NULL || bucket->max_entries == 0) { - bo_gem->bo.size = size; - if (bo_gem->bo.size < page_size) - bo_gem->bo.size = page_size; + bo_size = size; + if (bo_size < page_size) + bo_size = page_size; } /* Get a buffer out of the cache if available */ @@ -295,7 +293,9 @@ dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, struct dri_gem_bo_bucket_entry *entry = bucket->head; struct drm_i915_gem_busy busy; - busy.handle = entry->gem_handle; + bo_gem = entry->bo_gem; + busy.handle = bo_gem->gem_handle; + ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy); alloc_from_cache = (ret == 0 && busy.busy == 0); @@ -305,8 +305,6 @@ dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, bucket->tail = &bucket->head; bucket->num_entries--; - bo_gem->gem_handle = entry->gem_handle; - bo_gem->bo.offset = entry->last_offset; free(entry); } } @@ -314,8 +312,13 @@ dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, if (!alloc_from_cache) { struct drm_gem_create create; + bo_gem = calloc(1, sizeof(*bo_gem)); + if (!bo_gem) + return NULL; + + bo_gem->bo.size = bo_size; memset(&create, 0, sizeof(create)); - create.size = bo_gem->bo.size; + create.size = bo_size; ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CREATE, &create); bo_gem->gem_handle = create.handle; @@ -323,10 +326,9 @@ dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, free(bo_gem); return NULL; } + bo_gem->bo.bufmgr = bufmgr; } - bo_gem->bo.virtual = NULL; - bo_gem->bo.bufmgr = bufmgr; bo_gem->name = name; bo_gem->refcount = 1; bo_gem->validate_index = -1; @@ -400,9 +402,6 @@ dri_gem_bo_unreference(dri_bo *bo) struct dri_gem_bo_bucket *bucket; int ret; - if (bo_gem->mapped) - munmap (bo_gem->virtual, bo->size); - if (bo_gem->relocs != NULL) { int i; @@ -413,6 +412,9 @@ dri_gem_bo_unreference(dri_bo *bo) free(bo_gem->relocs); } + DBG("bo_unreference final: %d (%s)\n", + bo_gem->gem_handle, bo_gem->name); + bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo->size); /* Put the buffer into our internal cache for reuse if we can. */ if (bucket != NULL && @@ -422,9 +424,14 @@ dri_gem_bo_unreference(dri_bo *bo) { struct dri_gem_bo_bucket_entry *entry; + bo_gem->name = 0; + bo_gem->validate_index = -1; + bo_gem->relocs = NULL; + bo_gem->reloc_target_bo = NULL; + bo_gem->reloc_count = 0; + entry = calloc(1, sizeof(*entry)); - entry->gem_handle = bo_gem->gem_handle; - entry->last_offset = bo->offset; + entry->bo_gem = bo_gem; entry->next = NULL; *bucket->tail = entry; @@ -441,12 +448,9 @@ dri_gem_bo_unreference(dri_bo *bo) "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n", bo_gem->gem_handle, bo_gem->name, strerror(-ret)); } + free(bo); } - DBG("bo_unreference final: %d (%s)\n", - bo_gem->gem_handle, bo_gem->name); - - free(bo); return; } } @@ -604,6 +608,7 @@ dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr) while ((entry = bucket->head) != NULL) { struct drm_gem_close close; + dri_bo_gem *bo_gem; int ret; bucket->head = entry->next; @@ -611,14 +616,19 @@ dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr) bucket->tail = &bucket->head; bucket->num_entries--; + bo_gem = entry->bo_gem; + if (bo_gem->mapped) + munmap (bo_gem->virtual, bo_gem->bo.size); + /* Close this object */ - close.handle = entry->gem_handle; + close.handle = bo_gem->gem_handle; ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close); if (ret != 0) { fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %s\n", strerror(-ret)); } + free(bo_gem); free(entry); } } -- cgit v1.2.3 From 5a55b48a410bb25666177c0ea8e5711ea2e3c795 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 5 Jun 2008 15:58:55 -0700 Subject: [libdrm/intel] Remove unused intel_validate_entry structure --- libdrm/intel/intel_bufmgr_gem.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/libdrm/intel/intel_bufmgr_gem.c b/libdrm/intel/intel_bufmgr_gem.c index 08cb5d6b..32e70912 100644 --- a/libdrm/intel/intel_bufmgr_gem.c +++ b/libdrm/intel/intel_bufmgr_gem.c @@ -57,11 +57,6 @@ typedef struct _dri_bo_gem dri_bo_gem; -struct intel_validate_entry { - dri_bo_gem *bo_gem; - struct drm_i915_op_arg bo_arg; -}; - struct dri_gem_bo_bucket_entry { dri_bo_gem *bo_gem; struct dri_gem_bo_bucket_entry *next; -- cgit v1.2.3 From 329e0862255e8ad27e2aa4e3755421a18ea1acc5 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 5 Jun 2008 16:05:35 -0700 Subject: [libdrm/intel] Eliminate extra dri_gem_bo_bucket_entry structure Place the buffer reuse links right into the dri_bo_gem object. --- libdrm/intel/intel_bufmgr_gem.c | 42 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/libdrm/intel/intel_bufmgr_gem.c b/libdrm/intel/intel_bufmgr_gem.c index 32e70912..6504ad6e 100644 --- a/libdrm/intel/intel_bufmgr_gem.c +++ b/libdrm/intel/intel_bufmgr_gem.c @@ -57,14 +57,8 @@ typedef struct _dri_bo_gem dri_bo_gem; -struct dri_gem_bo_bucket_entry { - dri_bo_gem *bo_gem; - struct dri_gem_bo_bucket_entry *next; -}; - struct dri_gem_bo_bucket { - struct dri_gem_bo_bucket_entry *head; - struct dri_gem_bo_bucket_entry **tail; + dri_bo_gem *head, **tail; /** * Limit on the number of entries in this bucket. * @@ -129,6 +123,9 @@ struct _dri_bo_gem { int reloc_count; /** Mapped address for the buffer */ void *virtual; + + /** free list */ + dri_bo_gem *next; }; static int @@ -285,22 +282,19 @@ dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name, /* Get a buffer out of the cache if available */ if (bucket != NULL && bucket->num_entries > 0) { - struct dri_gem_bo_bucket_entry *entry = bucket->head; struct drm_i915_gem_busy busy; - bo_gem = entry->bo_gem; + bo_gem = bucket->head; busy.handle = bo_gem->gem_handle; ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy); alloc_from_cache = (ret == 0 && busy.busy == 0); if (alloc_from_cache) { - bucket->head = entry->next; - if (entry->next == NULL) + bucket->head = bo_gem->next; + if (bo_gem->next == NULL) bucket->tail = &bucket->head; bucket->num_entries--; - - free(entry); } } @@ -417,20 +411,15 @@ dri_gem_bo_unreference(dri_bo *bo) (bucket->max_entries > 0 && bucket->num_entries < bucket->max_entries))) { - struct dri_gem_bo_bucket_entry *entry; - bo_gem->name = 0; bo_gem->validate_index = -1; bo_gem->relocs = NULL; bo_gem->reloc_target_bo = NULL; bo_gem->reloc_count = 0; - entry = calloc(1, sizeof(*entry)); - entry->bo_gem = bo_gem; - - entry->next = NULL; - *bucket->tail = entry; - bucket->tail = &entry->next; + bo_gem->next = NULL; + *bucket->tail = bo_gem; + bucket->tail = &bo_gem->next; bucket->num_entries++; } else { struct drm_gem_close close; @@ -599,19 +588,17 @@ dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr) /* Free any cached buffer objects we were going to reuse */ for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) { struct dri_gem_bo_bucket *bucket = &bufmgr_gem->cache_bucket[i]; - struct dri_gem_bo_bucket_entry *entry; + dri_bo_gem *bo_gem; - while ((entry = bucket->head) != NULL) { + while ((bo_gem = bucket->head) != NULL) { struct drm_gem_close close; - dri_bo_gem *bo_gem; int ret; - bucket->head = entry->next; - if (entry->next == NULL) + bucket->head = bo_gem->next; + if (bo_gem->next == NULL) bucket->tail = &bucket->head; bucket->num_entries--; - bo_gem = entry->bo_gem; if (bo_gem->mapped) munmap (bo_gem->virtual, bo_gem->bo.size); @@ -624,7 +611,6 @@ dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr) } free(bo_gem); - free(entry); } } -- 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 +++++++---- shared-core/i915_dma.c | 1 + shared-core/i915_drm.h | 2 ++ shared-core/i915_drv.h | 2 ++ 4 files changed, 12 insertions(+), 4 deletions(-) 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; diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 1ea5c28e..6e188f08 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1189,6 +1189,7 @@ struct drm_ioctl_desc i915_ioctls[] = { DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH), + DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH), }; int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls); diff --git a/shared-core/i915_drm.h b/shared-core/i915_drm.h index 4712ea4f..0fa292d0 100644 --- a/shared-core/i915_drm.h +++ b/shared-core/i915_drm.h @@ -181,6 +181,7 @@ typedef struct drm_i915_sarea { #define DRM_I915_GEM_PIN 0x15 #define DRM_I915_GEM_UNPIN 0x16 #define DRM_I915_GEM_BUSY 0x17 +#define DRM_I915_GEM_THROTTLE 0x18 #define DRM_IOCTL_I915_INIT DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t) #define DRM_IOCTL_I915_FLUSH DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH) @@ -205,6 +206,7 @@ typedef struct drm_i915_sarea { #define DRM_IOCTL_I915_GEM_PIN DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_PIN, struct drm_i915_gem_pin) #define DRM_IOCTL_I915_GEM_UNPIN DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_UNPIN, struct drm_i915_gem_unpin) #define DRM_IOCTL_I915_GEM_BUSY DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_BUSY, struct drm_i915_gem_busy) +#define DRM_IOCTL_I915_GEM_THROTTLE DRM_IO ( DRM_COMMAND_BASE + DRM_I915_GEM_THROTTLE) /* Asynchronous page flipping: */ diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index d646177b..33fb7ca9 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -451,6 +451,8 @@ int i915_gem_unpin_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); int i915_gem_busy_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv); +int i915_gem_throttle_ioctl(struct drm_device *dev, void *data, + struct drm_file *file_priv); int i915_gem_init_object(struct drm_gem_object *obj); void i915_gem_free_object(struct drm_gem_object *obj); int i915_gem_set_domain(struct drm_gem_object *obj, -- cgit v1.2.3 From a708106c77f74f146722fba35eae772fb554ee9a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 6 Jun 2008 12:58:41 -0700 Subject: [intel] free the hardware status page at driver_unload This goes with the other hardware status page patch. --- shared-core/i915_dma.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index 6e188f08..c0ddeae0 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1103,8 +1103,9 @@ int i915_driver_unload(struct drm_device *dev) { struct drm_i915_private *dev_priv = dev->dev_private; - if (dev_priv->mmio_map) - drm_rmmap(dev, dev_priv->mmio_map); + i915_free_hardware_status(dev); + + drm_rmmap(dev, dev_priv->mmio_map); drm_free(dev->dev_private, sizeof(drm_i915_private_t), DRM_MEM_DRIVER); @@ -1113,8 +1114,6 @@ int i915_driver_unload(struct drm_device *dev) intel_fini_chipset_flush_compat(dev); #endif #endif - i915_free_hardware_status(dev); - return 0; } -- 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 +++++++++++++++++++++++++++++ shared-core/i915_dma.c | 5 +++++ shared-core/i915_drv.h | 12 ++++++++++++ 4 files changed, 47 insertions(+) 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. diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index c0ddeae0..f6465bf6 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -1078,6 +1078,11 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags) INIT_LIST_HEAD(&dev_priv->mm.flushing_list); INIT_LIST_HEAD(&dev_priv->mm.inactive_list); INIT_LIST_HEAD(&dev_priv->mm.request_list); + dev_priv->mm.retire_timer.function = i915_gem_retire_timeout; + dev_priv->mm.retire_timer.data = (unsigned long) dev; + init_timer_deferrable (&dev_priv->mm.retire_timer); + INIT_WORK(&dev_priv->mm.retire_task, + i915_gem_retire_handler); INIT_WORK(&dev_priv->user_interrupt_task, i915_user_interrupt_handler); dev_priv->mm.next_gem_seqno = 1; diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 33fb7ca9..55af6552 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -280,6 +280,16 @@ typedef struct drm_i915_private { */ struct list_head request_list; + /** + * We leave the user IRQ off as much as possible, + * but this means that requests will finish and never + * be retired once the system goes idle. Set a timer to + * fire periodically while the ring is running. When it + * fires, go retire requests. + */ + struct timer_list retire_timer; + struct work_struct retire_task; + uint32_t next_gem_seqno; } mm; @@ -463,6 +473,8 @@ int i915_gem_flush_pwrite(struct drm_gem_object *obj, uint64_t offset, uint64_t size); void i915_gem_lastclose(struct drm_device *dev); void i915_gem_retire_requests(struct drm_device *dev); +void i915_gem_retire_timeout(unsigned long data); +void i915_gem_retire_handler(struct work_struct *work); #endif #ifdef __linux__ -- 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 ++++++++--------- shared-core/i915_dma.c | 22 ++++++++-------------- shared-core/i915_drv.h | 1 - 3 files changed, 16 insertions(+), 24 deletions(-) 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(); } } diff --git a/shared-core/i915_dma.c b/shared-core/i915_dma.c index f6465bf6..e5c6d0c4 100644 --- a/shared-core/i915_dma.c +++ b/shared-core/i915_dma.c @@ -285,9 +285,6 @@ static int i915_initialize(struct drm_device * dev, /* We are using separate values as placeholders for mechanisms for * private backbuffer/depthbuffer usage. */ - dev_priv->use_mi_batchbuffer_start = 0; - if (IS_I965G(dev)) /* 965 doesn't support older method */ - dev_priv->use_mi_batchbuffer_start = 1; /* Allow hardware batchbuffers unless told otherwise. */ @@ -639,7 +636,14 @@ int i915_dispatch_batchbuffer(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(batch->start | MI_BATCH_NON_SECURE); + OUT_RING(batch->start + batch->used - 4); + OUT_RING(0); + ADVANCE_LP_RING(); + } else { BEGIN_LP_RING(2); if (IS_I965G(dev)) { OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965); @@ -649,14 +653,6 @@ int i915_dispatch_batchbuffer(struct drm_device * dev, OUT_RING(batch->start | MI_BATCH_NON_SECURE); } ADVANCE_LP_RING(); - - } else { - BEGIN_LP_RING(4); - OUT_RING(MI_BATCH_BUFFER); - OUT_RING(batch->start | MI_BATCH_NON_SECURE); - OUT_RING(batch->start + batch->used - 4); - OUT_RING(0); - ADVANCE_LP_RING(); } } @@ -930,8 +926,6 @@ static int i915_setparam(struct drm_device *dev, void *data, switch (param->param) { case I915_SETPARAM_USE_MI_BATCHBUFFER_START: - if (!IS_I965G(dev)) - dev_priv->use_mi_batchbuffer_start = param->value; break; case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY: dev_priv->tex_lru_log_granularity = param->value; diff --git a/shared-core/i915_drv.h b/shared-core/i915_drv.h index 55af6552..8acef0ca 100644 --- a/shared-core/i915_drv.h +++ b/shared-core/i915_drv.h @@ -117,7 +117,6 @@ typedef struct drm_i915_private { drm_local_map_t hws_map; unsigned int cpp; - int use_mi_batchbuffer_start; wait_queue_head_t irq_queue; atomic_t irq_received; -- cgit v1.2.3 From 500c81d194115fb3c4b97d742519689478eeb4e8 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 6 Jun 2008 17:13:16 -0700 Subject: [gem] Don't forget to munmap in the non-bo-reuse object-freeing case. --- libdrm/intel/intel_bufmgr_gem.c | 51 +++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/libdrm/intel/intel_bufmgr_gem.c b/libdrm/intel/intel_bufmgr_gem.c index 6504ad6e..a65ae982 100644 --- a/libdrm/intel/intel_bufmgr_gem.c +++ b/libdrm/intel/intel_bufmgr_gem.c @@ -378,6 +378,28 @@ dri_gem_bo_reference(dri_bo *bo) bo_gem->refcount++; } +static void +dri_gem_bo_free(dri_bo *bo) +{ + dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr; + dri_bo_gem *bo_gem = (dri_bo_gem *)bo; + struct drm_gem_close close; + int ret; + + if (bo_gem->mapped) + munmap (bo_gem->virtual, bo_gem->bo.size); + + /* Close this object */ + close.handle = bo_gem->gem_handle; + ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close); + if (ret != 0) { + fprintf(stderr, + "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n", + bo_gem->gem_handle, bo_gem->name, strerror(-ret)); + } + free(bo); +} + static void dri_gem_bo_unreference(dri_bo *bo) { @@ -389,7 +411,6 @@ dri_gem_bo_unreference(dri_bo *bo) if (--bo_gem->refcount == 0) { struct dri_gem_bo_bucket *bucket; - int ret; if (bo_gem->relocs != NULL) { int i; @@ -422,17 +443,7 @@ dri_gem_bo_unreference(dri_bo *bo) bucket->tail = &bo_gem->next; bucket->num_entries++; } else { - struct drm_gem_close close; - - /* Close this object */ - close.handle = bo_gem->gem_handle; - ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close); - if (ret != 0) { - fprintf(stderr, - "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n", - bo_gem->gem_handle, bo_gem->name, strerror(-ret)); - } - free(bo); + dri_gem_bo_free(bo); } return; @@ -591,26 +602,12 @@ dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr) dri_bo_gem *bo_gem; while ((bo_gem = bucket->head) != NULL) { - struct drm_gem_close close; - int ret; - bucket->head = bo_gem->next; if (bo_gem->next == NULL) bucket->tail = &bucket->head; bucket->num_entries--; - if (bo_gem->mapped) - munmap (bo_gem->virtual, bo_gem->bo.size); - - /* Close this object */ - close.handle = bo_gem->gem_handle; - ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close); - if (ret != 0) { - fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %s\n", - strerror(-ret)); - } - - free(bo_gem); + dri_gem_bo_free(&bo_gem->bo); } } -- cgit v1.2.3