xref: /freebsd/sys/dev/drm2/drm_vm.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
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 __FBSDID("$FreeBSD$");
38 
39 /** @file drm_vm.c
40  * Support code for mmaping of DRM maps.
41  */
42 
43 #include <dev/drm2/drmP.h>
44 #include <dev/drm2/drm.h>
45 
46 #ifdef FREEBSD_NOTYET
47 int
48 drm_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
49     int prot, vm_memattr_t *memattr)
50 {
51 	struct drm_device *dev = drm_get_device_from_kdev(kdev);
52 	struct drm_file *file_priv = NULL;
53 	drm_local_map_t *map;
54 	enum drm_map_type type;
55 	vm_paddr_t phys;
56 	int error;
57 
58 	/* d_mmap gets called twice, we can only reference file_priv during
59 	 * the first call.  We need to assume that if error is EBADF the
60 	 * call was successful and the client is authenticated.
61 	 */
62 	error = devfs_get_cdevpriv((void **)&file_priv);
63 	if (error == ENOENT) {
64 		DRM_ERROR("Could not find authenticator!\n");
65 		return EINVAL;
66 	}
67 
68 	if (file_priv && !file_priv->authenticated)
69 		return EACCES;
70 
71 	DRM_DEBUG("called with offset %016jx\n", offset);
72 	if (dev->dma && offset < ptoa(dev->dma->page_count)) {
73 		drm_device_dma_t *dma = dev->dma;
74 
75 		DRM_SPINLOCK(&dev->dma_lock);
76 
77 		if (dma->pagelist != NULL) {
78 			unsigned long page = offset >> PAGE_SHIFT;
79 			unsigned long phys = dma->pagelist[page];
80 
81 			DRM_SPINUNLOCK(&dev->dma_lock);
82 			*paddr = phys;
83 			return 0;
84 		} else {
85 			DRM_SPINUNLOCK(&dev->dma_lock);
86 			return -1;
87 		}
88 	}
89 
90 	/* A sequential search of a linked list is
91 	   fine here because: 1) there will only be
92 	   about 5-10 entries in the list and, 2) a
93 	   DRI client only has to do this mapping
94 	   once, so it doesn't have to be optimized
95 	   for performance, even if the list was a
96 	   bit longer.
97 	*/
98 	DRM_LOCK(dev);
99 	TAILQ_FOREACH(map, &dev->maplist, link) {
100 		if (offset >> DRM_MAP_HANDLE_SHIFT ==
101 		    (unsigned long)map->handle >> DRM_MAP_HANDLE_SHIFT)
102 			break;
103 	}
104 
105 	if (map == NULL) {
106 		DRM_DEBUG("Can't find map, request offset = %016jx\n", offset);
107 		TAILQ_FOREACH(map, &dev->maplist, link) {
108 			DRM_DEBUG("map offset = %016lx, handle = %016lx\n",
109 			    map->offset, (unsigned long)map->handle);
110 		}
111 		DRM_UNLOCK(dev);
112 		return -1;
113 	}
114 	if (((map->flags & _DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
115 		DRM_UNLOCK(dev);
116 		DRM_DEBUG("restricted map\n");
117 		return -1;
118 	}
119 	type = map->type;
120 	DRM_UNLOCK(dev);
121 
122 	offset = offset & ((1ULL << DRM_MAP_HANDLE_SHIFT) - 1);
123 
124 	switch (type) {
125 	case _DRM_FRAME_BUFFER:
126 	case _DRM_AGP:
127 		*memattr = VM_MEMATTR_WRITE_COMBINING;
128 		/* FALLTHROUGH */
129 	case _DRM_REGISTERS:
130 		phys = map->offset + offset;
131 		break;
132 	case _DRM_SCATTER_GATHER:
133 		*memattr = VM_MEMATTR_WRITE_COMBINING;
134 		/* FALLTHROUGH */
135 	case _DRM_CONSISTENT:
136 	case _DRM_SHM:
137 		phys = vtophys((char *)map->virtual + offset);
138 		break;
139 	default:
140 		DRM_ERROR("bad map type %d\n", type);
141 		return -1;	/* This should never happen. */
142 	}
143 
144 	*paddr = phys;
145 	return 0;
146 }
147 #endif /* FREEBSD_NOTYET */
148