xref: /dragonfly/sys/dev/drm/drm_vm.c (revision 394ce297)
1 /*-
2  * Copyright 2003 Eric Anholt
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * $FreeBSD: head/sys/dev/drm2/drm_vm.c 235783 2012-05-22 11:07:44Z kib $"
24  */
25 
26 /** @file drm_vm.c
27  * Support code for mmaping of DRM maps.
28  */
29 
30 #include <sys/conf.h>
31 #include <sys/devfs.h>
32 #include <sys/mutex2.h>
33 #include <vm/vm_page.h>
34 #include <vm/vm_pager.h>
35 
36 #include <drm/drmP.h>
37 #include <asm/pgtable.h>
38 #include "drm_legacy.h"
39 
40 int drm_mmap(struct dev_mmap_args *ap)
41 {
42 	struct cdev *kdev = ap->a_head.a_dev;
43 	vm_offset_t offset = ap->a_offset;
44 	struct drm_device *dev = drm_get_device_from_kdev(kdev);
45 	struct drm_file *file_priv = NULL;
46 	struct drm_local_map *map = NULL;
47 	int error;
48 	struct drm_hash_item *hash;
49 
50 	enum drm_map_type type;
51 	vm_paddr_t phys;
52 
53 	/* d_mmap gets called twice, we can only reference file_priv during
54 	 * the first call.  We need to assume that if error is EBADF the
55 	 * call was succesful and the client is authenticated.
56 	 */
57 	error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
58 	if (error == ENOENT) {
59 		DRM_ERROR("Could not find authenticator!\n");
60 		return EINVAL;
61 	}
62 
63 	if (file_priv && !file_priv->authenticated)
64 		return EACCES;
65 
66 	DRM_DEBUG("called with offset %016jx\n", (uintmax_t)offset);
67 	if (dev->dma && offset < ptoa(dev->dma->page_count)) {
68 		struct drm_device_dma *dma = dev->dma;
69 
70 		if (dma->pagelist != NULL) {
71 			unsigned long page = offset >> PAGE_SHIFT;
72 			unsigned long phys = dma->pagelist[page];
73 
74 			// XXX *paddr = phys;
75 			ap->a_result = phys;
76 			return 0;
77 		} else {
78 			return -1;
79 		}
80 	}
81 
82 	/* A sequential search of a linked list is
83 	   fine here because: 1) there will only be
84 	   about 5-10 entries in the list and, 2) a
85 	   DRI client only has to do this mapping
86 	   once, so it doesn't have to be optimized
87 	   for performance, even if the list was a
88 	   bit longer.
89 	*/
90 	DRM_LOCK(dev);
91 
92 	if (drm_ht_find_item(&dev->map_hash, offset, &hash)) {
93 		DRM_ERROR("Could not find map\n");
94 		return -EINVAL;
95 	}
96 
97 	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
98 	if (map == NULL) {
99 		DRM_DEBUG("Can't find map, request offset = %016jx\n",
100 		    (uintmax_t)offset);
101 		DRM_UNLOCK(dev);
102 		return -1;
103 	}
104 	if (((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
105 		DRM_UNLOCK(dev);
106 		DRM_DEBUG("restricted map\n");
107 		return -1;
108 	}
109 
110 	type = map->type;
111 	DRM_UNLOCK(dev);
112 
113 	switch (type) {
114 	case _DRM_FRAME_BUFFER:
115 	case _DRM_AGP:
116 #if 0	/* XXX */
117 		*memattr = VM_MEMATTR_WRITE_COMBINING;
118 #endif
119 		/* FALLTHROUGH */
120 	case _DRM_REGISTERS:
121 		phys = map->offset + offset;
122 		break;
123 	case _DRM_SCATTER_GATHER:
124 #if 0	/* XXX */
125 		*memattr = VM_MEMATTR_WRITE_COMBINING;
126 #endif
127 		/* FALLTHROUGH */
128 	case _DRM_CONSISTENT:
129 	case _DRM_SHM:
130 		phys = vtophys((char *)map->handle + offset);
131 		break;
132 	default:
133 		DRM_ERROR("bad map type %d\n", type);
134 		return -1;	/* This should never happen. */
135 	}
136 
137 	ap->a_result = atop(phys);
138 	return 0;
139 }
140 
141 /* XXX The following is just temporary hack to replace the
142  * vm_phys_fictitious functions available on FreeBSD
143  */
144 #define VM_PHYS_FICTITIOUS_NSEGS        8
145 static struct vm_phys_fictitious_seg {
146         vm_paddr_t      start;
147         vm_paddr_t      end;
148         vm_page_t       first_page;
149 } vm_phys_fictitious_segs[VM_PHYS_FICTITIOUS_NSEGS];
150 static struct mtx vm_phys_fictitious_reg_mtx = MTX_INITIALIZER("vmphy");
151 
152 vm_page_t
153 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
154 {
155         struct vm_phys_fictitious_seg *seg;
156         vm_page_t m;
157         int segind;
158 
159         m = NULL;
160         for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
161                 seg = &vm_phys_fictitious_segs[segind];
162                 if (pa >= seg->start && pa < seg->end) {
163                         m = &seg->first_page[atop(pa - seg->start)];
164                         KASSERT((m->flags & PG_FICTITIOUS) != 0,
165                             ("%p not fictitious", m));
166                         break;
167                 }
168         }
169         return (m);
170 }
171 
172 int
173 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
174     vm_memattr_t memattr)
175 {
176         struct vm_phys_fictitious_seg *seg;
177         vm_page_t fp;
178         long i, page_count;
179         int segind;
180 
181         page_count = (end - start) / PAGE_SIZE;
182 
183         fp = kmalloc(page_count * sizeof(struct vm_page), M_DRM,
184                     M_WAITOK | M_ZERO);
185 
186         for (i = 0; i < page_count; i++) {
187 		vm_page_initfake(&fp[i], start + PAGE_SIZE * i, memattr);
188 		fp[i].flags &= ~PG_UNMANAGED;
189 		atomic_clear_int(&fp[i].busy_count, PBUSY_LOCKED);
190         }
191         mtx_lock(&vm_phys_fictitious_reg_mtx);
192         for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
193                 seg = &vm_phys_fictitious_segs[segind];
194                 if (seg->start == 0 && seg->end == 0) {
195                         seg->start = start;
196                         seg->end = end;
197                         seg->first_page = fp;
198                         mtx_unlock(&vm_phys_fictitious_reg_mtx);
199                         return (0);
200                 }
201         }
202         mtx_unlock(&vm_phys_fictitious_reg_mtx);
203         kfree(fp);
204         return (EBUSY);
205 }
206 
207 void
208 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
209 {
210 	struct vm_phys_fictitious_seg *seg;
211 	vm_page_t fp;
212 	int segind;
213 
214 	mtx_lock(&vm_phys_fictitious_reg_mtx);
215 	for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
216 		seg = &vm_phys_fictitious_segs[segind];
217 		if (seg->start == start && seg->end == end) {
218 			seg->start = seg->end = 0;
219 			fp = seg->first_page;
220 			seg->first_page = NULL;
221 			mtx_unlock(&vm_phys_fictitious_reg_mtx);
222 			kfree(fp);
223 			return;
224 		}
225 	}
226 	mtx_unlock(&vm_phys_fictitious_reg_mtx);
227 	KASSERT(0, ("Unregistering not registered fictitious range"));
228 }
229