xref: /openbsd-src/sys/uvm/uvm_device.c (revision 24bb5fcea3ed904bc467217bdaadb5dfc618d5bf)
1 /*	$OpenBSD: uvm_device.c,v 1.64 2021/06/29 01:46:35 jsg Exp $	*/
2 /*	$NetBSD: uvm_device.c,v 1.30 2000/11/25 06:27:59 chs Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * from: Id: uvm_device.c,v 1.1.2.9 1998/02/06 05:11:47 chs Exp
29  */
30 
31 /*
32  * uvm_device.c: the device pager.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 
41 #include <uvm/uvm.h>
42 #include <uvm/uvm_device.h>
43 
44 #include "drm.h"
45 
46 /*
47  * private global data structure
48  *
49  * we keep a list of active device objects in the system.
50  */
51 
52 LIST_HEAD(, uvm_device) udv_list = LIST_HEAD_INITIALIZER(udv_list);
53 struct mutex udv_lock = MUTEX_INITIALIZER(IPL_NONE);
54 
55 /*
56  * functions
57  */
58 static void             udv_reference(struct uvm_object *);
59 static void             udv_detach(struct uvm_object *);
60 static int		udv_fault(struct uvm_faultinfo *, vaddr_t,
61 				       vm_page_t *, int, int, vm_fault_t,
62 				       vm_prot_t, int);
63 static boolean_t        udv_flush(struct uvm_object *, voff_t, voff_t,
64 				       int);
65 
66 /*
67  * master pager structure
68  */
69 const struct uvm_pagerops uvm_deviceops = {
70 	.pgo_reference = udv_reference,
71 	.pgo_detach = udv_detach,
72 	.pgo_fault = udv_fault,
73 	.pgo_flush = udv_flush,
74 };
75 
76 /*
77  * the ops!
78  */
79 
80 
81 /*
82  * udv_attach
83  *
84  * get a VM object that is associated with a device.   allocate a new
85  * one if needed.
86  *
87  * => nothing should be locked so that we can sleep here.
88  *
89  * The last two arguments (off and size) are only used for access checking.
90  */
91 struct uvm_object *
92 udv_attach(dev_t device, vm_prot_t accessprot, voff_t off, vsize_t size)
93 {
94 	struct uvm_device *udv, *lcv;
95 	paddr_t (*mapfn)(dev_t, off_t, int);
96 #if NDRM > 0
97 	struct uvm_object *obj;
98 #endif
99 
100 	/*
101 	 * before we do anything, ensure this device supports mmap
102 	 */
103 	mapfn = cdevsw[major(device)].d_mmap;
104 	if (mapfn == NULL ||
105 	    mapfn == (paddr_t (*)(dev_t, off_t, int)) enodev ||
106 	    mapfn == (paddr_t (*)(dev_t, off_t, int)) nullop)
107 		return(NULL);
108 
109 	/*
110 	 * Negative offsets on the object are not allowed.
111 	 */
112 	if (off < 0)
113 		return(NULL);
114 
115 #if NDRM > 0
116 	obj = udv_attach_drm(device, accessprot, off, size);
117 	if (obj)
118 		return(obj);
119 #endif
120 
121 	/*
122 	 * Check that the specified range of the device allows the
123 	 * desired protection.
124 	 *
125 	 * XXX clobbers off and size, but nothing else here needs them.
126 	 */
127 	while (size != 0) {
128 		if ((*mapfn)(device, off, accessprot) == -1)
129 			return (NULL);
130 		off += PAGE_SIZE; size -= PAGE_SIZE;
131 	}
132 
133 	/*
134 	 * keep looping until we get it
135 	 */
136 	for (;;) {
137 		/*
138 		 * first, attempt to find it on the main list
139 		 */
140 		mtx_enter(&udv_lock);
141 		LIST_FOREACH(lcv, &udv_list, u_list) {
142 			if (device == lcv->u_device)
143 				break;
144 		}
145 
146 		/*
147 		 * got it on main list.  put a hold on it and unlock udv_lock.
148 		 */
149 		if (lcv) {
150 			/*
151 			 * if someone else has a hold on it, sleep and start
152 			 * over again. Else, we need take HOLD flag so we
153 			 * don't have to re-order locking here.
154 			 */
155 			if (lcv->u_flags & UVM_DEVICE_HOLD) {
156 				lcv->u_flags |= UVM_DEVICE_WANTED;
157 				msleep_nsec(lcv, &udv_lock, PVM | PNORELOCK,
158 				    "udv_attach", INFSLP);
159 				continue;
160 			}
161 
162 			/* we are now holding it */
163 			lcv->u_flags |= UVM_DEVICE_HOLD;
164 			mtx_leave(&udv_lock);
165 
166 			/*
167 			 * bump reference count, unhold, return.
168 			 */
169 			lcv->u_obj.uo_refs++;
170 
171 			mtx_enter(&udv_lock);
172 			if (lcv->u_flags & UVM_DEVICE_WANTED)
173 				wakeup(lcv);
174 			lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
175 			mtx_leave(&udv_lock);
176 			return(&lcv->u_obj);
177 		}
178 
179 		/*
180 		 * Did not find it on main list.  Need to allocate a new one.
181 		 */
182 		mtx_leave(&udv_lock);
183 		/* NOTE: we could sleep in the following malloc() */
184 		udv = malloc(sizeof(*udv), M_TEMP, M_WAITOK);
185 		mtx_enter(&udv_lock);
186 
187 		/*
188 		 * now we have to double check to make sure no one added it
189 		 * to the list while we were sleeping...
190 		 */
191 		LIST_FOREACH(lcv, &udv_list, u_list) {
192 			if (device == lcv->u_device)
193 				break;
194 		}
195 
196 		/*
197 		 * did we lose a race to someone else?
198 		 * free our memory and retry.
199 		 */
200 		if (lcv) {
201 			mtx_leave(&udv_lock);
202 			free(udv, M_TEMP, sizeof(*udv));
203 			continue;
204 		}
205 
206 		/*
207 		 * we have it!   init the data structures, add to list
208 		 * and return.
209 		 */
210 		uvm_obj_init(&udv->u_obj, &uvm_deviceops, 1);
211 		udv->u_flags = 0;
212 		udv->u_device = device;
213 		LIST_INSERT_HEAD(&udv_list, udv, u_list);
214 		mtx_leave(&udv_lock);
215 		return(&udv->u_obj);
216 	}
217 	/*NOTREACHED*/
218 }
219 
220 /*
221  * udv_reference
222  *
223  * add a reference to a VM object.   Note that the reference count must
224  * already be one (the passed in reference) so there is no chance of the
225  * udv being released or locked out here.
226  */
227 static void
228 udv_reference(struct uvm_object *uobj)
229 {
230 	KERNEL_ASSERT_LOCKED();
231 	uobj->uo_refs++;
232 }
233 
234 /*
235  * udv_detach
236  *
237  * remove a reference to a VM object.
238  */
239 static void
240 udv_detach(struct uvm_object *uobj)
241 {
242 	struct uvm_device *udv = (struct uvm_device *)uobj;
243 
244 	KERNEL_ASSERT_LOCKED();
245 
246 	/*
247 	 * loop until done
248 	 */
249 again:
250 	if (uobj->uo_refs > 1) {
251 		uobj->uo_refs--;
252 		return;
253 	}
254 	KASSERT(uobj->uo_npages == 0 && RBT_EMPTY(uvm_objtree, &uobj->memt));
255 
256 	/*
257 	 * is it being held?   if so, wait until others are done.
258 	 */
259 	mtx_enter(&udv_lock);
260 	if (udv->u_flags & UVM_DEVICE_HOLD) {
261 		udv->u_flags |= UVM_DEVICE_WANTED;
262 		/*
263 		 * lock interleaving. -- this is ok in this case since the
264 		 * locks are both IPL_NONE
265 		 */
266 		msleep_nsec(udv, &udv_lock, PVM | PNORELOCK, "udv_detach",
267 		    INFSLP);
268 		goto again;
269 	}
270 
271 	/*
272 	 * got it!   nuke it now.
273 	 */
274 	LIST_REMOVE(udv, u_list);
275 	if (udv->u_flags & UVM_DEVICE_WANTED)
276 		wakeup(udv);
277 	mtx_leave(&udv_lock);
278 	free(udv, M_TEMP, sizeof(*udv));
279 }
280 
281 
282 /*
283  * udv_flush
284  *
285  * flush pages out of a uvm object.   a no-op for devices.
286  */
287 static boolean_t
288 udv_flush(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
289 {
290 
291 	return(TRUE);
292 }
293 
294 /*
295  * udv_fault: non-standard fault routine for device "pages"
296  *
297  * => rather than having a "get" function, we have a fault routine
298  *	since we don't return vm_pages we need full control over the
299  *	pmap_enter map in
300  * => on return, we unlock all fault data structures
301  * => flags: PGO_ALLPAGES: get all of the pages
302  *	     PGO_LOCKED: fault data structures are locked
303  *    XXX: currently PGO_LOCKED is always required ... consider removing
304  *	it as a flag
305  * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
306  */
307 static int
308 udv_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, vm_page_t *pps, int npages,
309     int centeridx, vm_fault_t fault_type, vm_prot_t access_type, int flags)
310 {
311 	struct vm_map_entry *entry = ufi->entry;
312 	struct uvm_object *uobj = entry->object.uvm_obj;
313 	struct uvm_device *udv = (struct uvm_device *)uobj;
314 	vaddr_t curr_va;
315 	off_t curr_offset;
316 	paddr_t paddr;
317 	int lcv, retval;
318 	dev_t device;
319 	paddr_t (*mapfn)(dev_t, off_t, int);
320 	vm_prot_t mapprot;
321 
322 	KERNEL_ASSERT_LOCKED();
323 
324 	/*
325 	 * we do not allow device mappings to be mapped copy-on-write
326 	 * so we kill any attempt to do so here.
327 	 */
328 	if (UVM_ET_ISCOPYONWRITE(entry)) {
329 		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
330 		return(VM_PAGER_ERROR);
331 	}
332 
333 	/*
334 	 * get device map function.
335 	 */
336 	device = udv->u_device;
337 	mapfn = cdevsw[major(device)].d_mmap;
338 
339 	/*
340 	 * now we must determine the offset in udv to use and the VA to
341 	 * use for pmap_enter.  note that we always use orig_map's pmap
342 	 * for pmap_enter (even if we have a submap).   since virtual
343 	 * addresses in a submap must match the main map, this is ok.
344 	 */
345 	/* udv offset = (offset from start of entry) + entry's offset */
346 	curr_offset = entry->offset + (vaddr - entry->start);
347 	/* pmap va = vaddr (virtual address of pps[0]) */
348 	curr_va = vaddr;
349 
350 	/*
351 	 * loop over the page range entering in as needed
352 	 */
353 	retval = VM_PAGER_OK;
354 	for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
355 	    curr_va += PAGE_SIZE) {
356 		if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
357 			continue;
358 
359 		if (pps[lcv] == PGO_DONTCARE)
360 			continue;
361 
362 		paddr = (*mapfn)(device, curr_offset, access_type);
363 		if (paddr == -1) {
364 			retval = VM_PAGER_ERROR;
365 			break;
366 		}
367 		mapprot = ufi->entry->protection;
368 		if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr,
369 		    mapprot, PMAP_CANFAIL | mapprot) != 0) {
370 			/*
371 			 * pmap_enter() didn't have the resource to
372 			 * enter this mapping.  Unlock everything,
373 			 * wait for the pagedaemon to free up some
374 			 * pages, and then tell uvm_fault() to start
375 			 * the fault again.
376 			 *
377 			 * XXX Needs some rethinking for the PGO_ALLPAGES
378 			 * XXX case.
379 			 */
380 			uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
381 			    uobj);
382 
383 			/* sync what we have so far */
384 			pmap_update(ufi->orig_map->pmap);
385 			uvm_wait("udv_fault");
386 			return (VM_PAGER_REFAULT);
387 		}
388 	}
389 
390 	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
391 	pmap_update(ufi->orig_map->pmap);
392 	return (retval);
393 }
394