Home | History | Annotate | Line # | Download | only in drm
drm_gem_vm.c revision 1.1.2.4
      1 /*	$NetBSD: drm_gem_vm.c,v 1.1.2.4 2014/01/22 16:40:44 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: drm_gem_vm.c,v 1.1.2.4 2014/01/22 16:40:44 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 
     37 #include <uvm/uvm_extern.h>
     38 
     39 #include <drm/drmP.h>
     40 
     41 static int	drm_gem_mmap_object_locked(struct drm_device *, off_t, size_t,
     42 		    int, struct uvm_object **);
     43 
     44 void
     45 drm_gem_pager_reference(struct uvm_object *uobj)
     46 {
     47 	struct drm_gem_object *const obj = container_of(uobj,
     48 	    struct drm_gem_object, gemo_uvmobj);
     49 
     50 	drm_gem_object_reference(obj);
     51 }
     52 
     53 void
     54 drm_gem_pager_detach(struct uvm_object *uobj)
     55 {
     56 	struct drm_gem_object *const obj = container_of(uobj,
     57 	    struct drm_gem_object, gemo_uvmobj);
     58 
     59 	drm_gem_object_unreference_unlocked(obj);
     60 }
     61 
     62 int
     63 drm_gem_mmap_object(struct drm_device *dev, off_t byte_offset, size_t nbytes,
     64     int prot, struct uvm_object **uobjp)
     65 {
     66 	int ret;
     67 
     68 	mutex_lock(&dev->struct_mutex);
     69 	ret = drm_gem_mmap_object_locked(dev, byte_offset, nbytes, prot,
     70 	    uobjp);
     71 	mutex_unlock(&dev->struct_mutex);
     72 
     73 	return ret;
     74 }
     75 
     76 static int
     77 drm_gem_mmap_object_locked(struct drm_device *dev, off_t byte_offset,
     78     size_t nbytes, int prot __unused, struct uvm_object **uobjp)
     79 {
     80 	struct drm_gem_mm *const mm = dev->mm_private;
     81 	const off_t page_offset = (byte_offset >> PAGE_SHIFT);
     82 	struct drm_hash_item *hash;
     83 
     84 	KASSERT(mutex_is_locked(&dev->struct_mutex));
     85 	KASSERT(drm_core_check_feature(dev, DRIVER_GEM));
     86 	KASSERT(dev->driver->gem_uvm_ops != NULL);
     87 	KASSERT(prot == (prot & (PROT_READ | PROT_WRITE)));
     88 
     89 	if (byte_offset != (byte_offset & ~(PAGE_SIZE-1))) /* XXX kassert?  */
     90 		return -EINVAL;
     91 
     92 	if (drm_ht_find_item(&mm->offset_hash, page_offset, &hash) != 0) {
     93 		/* Fall back to vanilla device mappings.  */
     94 		*uobjp = NULL;
     95 		return 0;
     96 	}
     97 
     98 	struct drm_local_map *const map = drm_hash_entry(hash,
     99 	    struct drm_map_list, hash)->map;
    100 
    101 	if (map == NULL)	/* XXX How can this happen?  */
    102 		return -EINVAL;
    103 	if (map->size < nbytes)
    104 		return -EOVERFLOW;
    105 
    106 	struct drm_gem_object *const obj = map->handle;
    107 	KASSERT(obj->dev == dev);
    108 
    109 	/* Success!  */
    110 	drm_gem_object_reference(obj); /* XXX Locking?  */
    111 	*uobjp = &obj->gemo_uvmobj;
    112 	return 0;
    113 }
    114