xref: /freebsd/sys/dev/drm2/drm_vm.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /**
2  * \file drm_vm.c
3  * Memory mapping for DRM
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8 
9 /*
10  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
11  *
12  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14  * All Rights Reserved.
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice (including the next
24  * paragraph) shall be included in all copies or substantial portions of the
25  * Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  */
35 
36 #include <sys/cdefs.h>
37 /** @file drm_vm.c
38  * Support code for mmaping of DRM maps.
39  */
40 
41 #include <dev/drm2/drmP.h>
42 #include <dev/drm2/drm.h>
43 
44 #ifdef FREEBSD_NOTYET
45 int
46 drm_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
47     int prot, vm_memattr_t *memattr)
48 {
49 	struct drm_device *dev = drm_get_device_from_kdev(kdev);
50 	struct drm_file *file_priv = NULL;
51 	drm_local_map_t *map;
52 	enum drm_map_type type;
53 	vm_paddr_t phys;
54 	int error;
55 
56 	/* d_mmap gets called twice, we can only reference file_priv during
57 	 * the first call.  We need to assume that if error is EBADF the
58 	 * call was successful and the client is authenticated.
59 	 */
60 	error = devfs_get_cdevpriv((void **)&file_priv);
61 	if (error == ENOENT) {
62 		DRM_ERROR("Could not find authenticator!\n");
63 		return EINVAL;
64 	}
65 
66 	if (file_priv && !file_priv->authenticated)
67 		return EACCES;
68 
69 	DRM_DEBUG("called with offset %016jx\n", offset);
70 	if (dev->dma && offset < ptoa(dev->dma->page_count)) {
71 		drm_device_dma_t *dma = dev->dma;
72 
73 		DRM_SPINLOCK(&dev->dma_lock);
74 
75 		if (dma->pagelist != NULL) {
76 			unsigned long page = offset >> PAGE_SHIFT;
77 			unsigned long phys = dma->pagelist[page];
78 
79 			DRM_SPINUNLOCK(&dev->dma_lock);
80 			*paddr = phys;
81 			return 0;
82 		} else {
83 			DRM_SPINUNLOCK(&dev->dma_lock);
84 			return -1;
85 		}
86 	}
87 
88 	/* A sequential search of a linked list is
89 	   fine here because: 1) there will only be
90 	   about 5-10 entries in the list and, 2) a
91 	   DRI client only has to do this mapping
92 	   once, so it doesn't have to be optimized
93 	   for performance, even if the list was a
94 	   bit longer.
95 	*/
96 	DRM_LOCK(dev);
97 	TAILQ_FOREACH(map, &dev->maplist, link) {
98 		if (offset >> DRM_MAP_HANDLE_SHIFT ==
99 		    (unsigned long)map->handle >> DRM_MAP_HANDLE_SHIFT)
100 			break;
101 	}
102 
103 	if (map == NULL) {
104 		DRM_DEBUG("Can't find map, request offset = %016jx\n", offset);
105 		TAILQ_FOREACH(map, &dev->maplist, link) {
106 			DRM_DEBUG("map offset = %016lx, handle = %016lx\n",
107 			    map->offset, (unsigned long)map->handle);
108 		}
109 		DRM_UNLOCK(dev);
110 		return -1;
111 	}
112 	if (((map->flags & _DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
113 		DRM_UNLOCK(dev);
114 		DRM_DEBUG("restricted map\n");
115 		return -1;
116 	}
117 	type = map->type;
118 	DRM_UNLOCK(dev);
119 
120 	offset = offset & ((1ULL << DRM_MAP_HANDLE_SHIFT) - 1);
121 
122 	switch (type) {
123 	case _DRM_FRAME_BUFFER:
124 	case _DRM_AGP:
125 		*memattr = VM_MEMATTR_WRITE_COMBINING;
126 		/* FALLTHROUGH */
127 	case _DRM_REGISTERS:
128 		phys = map->offset + offset;
129 		break;
130 	case _DRM_SCATTER_GATHER:
131 		*memattr = VM_MEMATTR_WRITE_COMBINING;
132 		/* FALLTHROUGH */
133 	case _DRM_CONSISTENT:
134 	case _DRM_SHM:
135 		phys = vtophys((char *)map->virtual + offset);
136 		break;
137 	default:
138 		DRM_ERROR("bad map type %d\n", type);
139 		return -1;	/* This should never happen. */
140 	}
141 
142 	*paddr = phys;
143 	return 0;
144 }
145 #endif /* FREEBSD_NOTYET */
146