xref: /dragonfly/sys/dev/drm/drm_vm.c (revision 0db87cb7)
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 
38 int drm_mmap(struct dev_mmap_args *ap)
39 {
40 	struct cdev *kdev = ap->a_head.a_dev;
41 	vm_offset_t offset = ap->a_offset;
42 	struct drm_device *dev = drm_get_device_from_kdev(kdev);
43 	struct drm_file *file_priv = NULL;
44 	struct drm_local_map *map = NULL;
45 	int error;
46 	struct drm_hash_item *hash;
47 
48 	enum drm_map_type type;
49 	vm_paddr_t phys;
50 
51 	/* d_mmap gets called twice, we can only reference file_priv during
52 	 * the first call.  We need to assume that if error is EBADF the
53 	 * call was succesful and the client is authenticated.
54 	 */
55 	error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
56 	if (error == ENOENT) {
57 		DRM_ERROR("Could not find authenticator!\n");
58 		return EINVAL;
59 	}
60 
61 	if (file_priv && !file_priv->authenticated)
62 		return EACCES;
63 
64 	DRM_DEBUG("called with offset %016jx\n", (uintmax_t)offset);
65 	if (dev->dma && offset < ptoa(dev->dma->page_count)) {
66 		drm_device_dma_t *dma = dev->dma;
67 
68 		spin_lock(&dev->dma_lock);
69 
70 		if (dma->pagelist != NULL) {
71 			unsigned long page = offset >> PAGE_SHIFT;
72 			unsigned long phys = dma->pagelist[page];
73 
74 			spin_unlock(&dev->dma_lock);
75 			// XXX *paddr = phys;
76 			ap->a_result = phys;
77 			return 0;
78 		} else {
79 			spin_unlock(&dev->dma_lock);
80 			return -1;
81 		}
82 	}
83 
84 	/* A sequential search of a linked list is
85 	   fine here because: 1) there will only be
86 	   about 5-10 entries in the list and, 2) a
87 	   DRI client only has to do this mapping
88 	   once, so it doesn't have to be optimized
89 	   for performance, even if the list was a
90 	   bit longer.
91 	*/
92 	DRM_LOCK(dev);
93 
94 	if (drm_ht_find_item(&dev->map_hash, offset, &hash)) {
95 		DRM_ERROR("Could not find map\n");
96 		return -EINVAL;
97 	}
98 
99 	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
100 	if (map == NULL) {
101 		DRM_DEBUG("Can't find map, request offset = %016jx\n",
102 		    (uintmax_t)offset);
103 		DRM_UNLOCK(dev);
104 		return -1;
105 	}
106 	if (((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
107 		DRM_UNLOCK(dev);
108 		DRM_DEBUG("restricted map\n");
109 		return -1;
110 	}
111 
112 	type = map->type;
113 	DRM_UNLOCK(dev);
114 
115 	switch (type) {
116 	case _DRM_FRAME_BUFFER:
117 	case _DRM_AGP:
118 #if 0	/* XXX */
119 		*memattr = VM_MEMATTR_WRITE_COMBINING;
120 #endif
121 		/* FALLTHROUGH */
122 	case _DRM_REGISTERS:
123 		phys = map->offset + offset;
124 		break;
125 	case _DRM_SCATTER_GATHER:
126 #if 0	/* XXX */
127 		*memattr = VM_MEMATTR_WRITE_COMBINING;
128 #endif
129 		/* FALLTHROUGH */
130 	case _DRM_CONSISTENT:
131 	case _DRM_SHM:
132 		phys = vtophys((char *)map->handle + offset);
133 		break;
134 	default:
135 		DRM_ERROR("bad map type %d\n", type);
136 		return -1;	/* This should never happen. */
137 	}
138 
139 	ap->a_result = atop(phys);
140 	return 0;
141 }
142 
143 /* XXX The following is just temporary hack to replace the
144  * vm_phys_fictitious functions available on FreeBSD
145  */
146 #define VM_PHYS_FICTITIOUS_NSEGS        8
147 static struct vm_phys_fictitious_seg {
148         vm_paddr_t      start;
149         vm_paddr_t      end;
150         vm_page_t       first_page;
151 } vm_phys_fictitious_segs[VM_PHYS_FICTITIOUS_NSEGS];
152 static struct mtx vm_phys_fictitious_reg_mtx = MTX_INITIALIZER("vmphy");
153 
154 vm_page_t
155 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
156 {
157         struct vm_phys_fictitious_seg *seg;
158         vm_page_t m;
159         int segind;
160 
161         m = NULL;
162         for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
163                 seg = &vm_phys_fictitious_segs[segind];
164                 if (pa >= seg->start && pa < seg->end) {
165                         m = &seg->first_page[atop(pa - seg->start)];
166                         KASSERT((m->flags & PG_FICTITIOUS) != 0,
167                             ("%p not fictitious", m));
168                         break;
169                 }
170         }
171         return (m);
172 }
173 
174 int
175 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
176     vm_memattr_t memattr)
177 {
178         struct vm_phys_fictitious_seg *seg;
179         vm_page_t fp;
180         long i, page_count;
181         int segind;
182 
183         page_count = (end - start) / PAGE_SIZE;
184 
185         fp = kmalloc(page_count * sizeof(struct vm_page), M_DRM,
186                     M_WAITOK | M_ZERO);
187 
188         for (i = 0; i < page_count; i++) {
189 		vm_page_initfake(&fp[i], start + PAGE_SIZE * i, memattr);
190 		fp[i].flags &= ~(PG_BUSY | PG_UNMANAGED);
191         }
192         mtx_lock(&vm_phys_fictitious_reg_mtx);
193         for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
194                 seg = &vm_phys_fictitious_segs[segind];
195                 if (seg->start == 0 && seg->end == 0) {
196                         seg->start = start;
197                         seg->end = end;
198                         seg->first_page = fp;
199                         mtx_unlock(&vm_phys_fictitious_reg_mtx);
200                         return (0);
201                 }
202         }
203         mtx_unlock(&vm_phys_fictitious_reg_mtx);
204         kfree(fp);
205         return (EBUSY);
206 }
207 
208 void
209 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
210 {
211 	struct vm_phys_fictitious_seg *seg;
212 	vm_page_t fp;
213 	int segind;
214 
215 	mtx_lock(&vm_phys_fictitious_reg_mtx);
216 	for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
217 		seg = &vm_phys_fictitious_segs[segind];
218 		if (seg->start == start && seg->end == end) {
219 			seg->start = seg->end = 0;
220 			fp = seg->first_page;
221 			seg->first_page = NULL;
222 			mtx_unlock(&vm_phys_fictitious_reg_mtx);
223 			kfree(fp);
224 			return;
225 		}
226 	}
227 	mtx_unlock(&vm_phys_fictitious_reg_mtx);
228 	KASSERT(0, ("Unregistering not registered fictitious range"));
229 }
230