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