1 /* $NetBSD: drm_gem_vm.c,v 1.6 2015/03/09 01:29:40 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.6 2015/03/09 01:29:40 riastradh Exp $"); 34 35 #include <sys/types.h> 36 37 #include <uvm/uvm_extern.h> 38 39 #include <drm/drmP.h> 40 #include <drm/drm_vma_manager.h> 41 42 static int drm_gem_mmap_object_locked(struct drm_device *, off_t, size_t, 43 int, struct uvm_object **, voff_t *, struct file *); 44 45 void 46 drm_gem_pager_reference(struct uvm_object *uobj) 47 { 48 struct drm_gem_object *const obj = container_of(uobj, 49 struct drm_gem_object, gemo_uvmobj); 50 51 drm_gem_object_reference(obj); 52 } 53 54 void 55 drm_gem_pager_detach(struct uvm_object *uobj) 56 { 57 struct drm_gem_object *const obj = container_of(uobj, 58 struct drm_gem_object, gemo_uvmobj); 59 60 drm_gem_object_unreference_unlocked(obj); 61 } 62 63 int 64 drm_gem_or_legacy_mmap_object(struct drm_device *dev, off_t byte_offset, 65 size_t nbytes, int prot, struct uvm_object **uobjp, voff_t *uoffsetp, 66 struct file *file) 67 { 68 int ret; 69 70 ret = drm_gem_mmap_object(dev, byte_offset, nbytes, prot, uobjp, 71 uoffsetp, file); 72 if (ret) 73 return ret; 74 if (*uobjp != NULL) 75 return 0; 76 77 return drm_mmap_object(dev, byte_offset, nbytes, prot, uobjp, 78 uoffsetp, file); 79 } 80 81 int 82 drm_gem_mmap_object(struct drm_device *dev, off_t byte_offset, size_t nbytes, 83 int prot, struct uvm_object **uobjp, voff_t *uoffsetp, struct file *file) 84 { 85 int ret; 86 87 mutex_lock(&dev->struct_mutex); 88 ret = drm_gem_mmap_object_locked(dev, byte_offset, nbytes, prot, 89 uobjp, uoffsetp, file); 90 mutex_unlock(&dev->struct_mutex); 91 92 return ret; 93 } 94 95 static int 96 drm_gem_mmap_object_locked(struct drm_device *dev, off_t byte_offset, 97 size_t nbytes, int prot __unused, struct uvm_object **uobjp, 98 voff_t *uoffsetp, struct file *file) 99 { 100 const unsigned long startpage = (byte_offset >> PAGE_SHIFT); 101 const unsigned long npages = (nbytes >> PAGE_SHIFT); 102 103 KASSERT(mutex_is_locked(&dev->struct_mutex)); 104 KASSERT(drm_core_check_feature(dev, DRIVER_GEM)); 105 KASSERT(dev->driver->gem_uvm_ops != NULL); 106 KASSERT(prot == (prot & (PROT_READ | PROT_WRITE))); 107 KASSERT(0 <= byte_offset); 108 KASSERT(byte_offset == (byte_offset & ~(PAGE_SIZE-1))); 109 KASSERT(nbytes == (npages << PAGE_SHIFT)); 110 111 struct drm_vma_offset_node *const node = 112 drm_vma_offset_exact_lookup(dev->vma_offset_manager, startpage, 113 npages); 114 if (node == NULL) { 115 /* Fall back to vanilla device mappings. */ 116 *uobjp = NULL; 117 *uoffsetp = (voff_t)-1; 118 return 0; 119 } 120 121 if (!drm_vma_node_is_allowed(node, file)) 122 return -EACCES; 123 124 struct drm_gem_object *const obj = container_of(node, 125 struct drm_gem_object, vma_node); 126 KASSERT(obj->dev == dev); 127 128 /* Success! */ 129 drm_gem_object_reference(obj); 130 *uobjp = &obj->gemo_uvmobj; 131 *uoffsetp = 0; 132 return 0; 133 } 134