xref: /openbsd/sys/uvm/uvm_device.c (revision 55cc5ba3)
1 /*	$OpenBSD: uvm_device.c,v 1.60 2020/11/06 11:52:39 mpi 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 #if defined(__amd64__) || defined(__arm64__) || \
45     defined(__i386__) || defined(__loongson__) || \
46     defined(__macppc__) || defined(__powerpc64__) || \
47     defined(__sparc64__)
48 #include "drm.h"
49 #endif
50 
51 /*
52  * private global data structure
53  *
54  * we keep a list of active device objects in the system.
55  */
56 
57 LIST_HEAD(, uvm_device) udv_list = LIST_HEAD_INITIALIZER(udv_list);
58 struct mutex udv_lock = MUTEX_INITIALIZER(IPL_NONE);
59 
60 /*
61  * functions
62  */
63 static void             udv_reference(struct uvm_object *);
64 static void             udv_detach(struct uvm_object *);
65 static int		udv_fault(struct uvm_faultinfo *, vaddr_t,
66 				       vm_page_t *, int, int, vm_fault_t,
67 				       vm_prot_t, int);
68 static boolean_t        udv_flush(struct uvm_object *, voff_t, voff_t,
69 				       int);
70 
71 /*
72  * master pager structure
73  */
74 const struct uvm_pagerops uvm_deviceops = {
75 	.pgo_reference = udv_reference,
76 	.pgo_detach = udv_detach,
77 	.pgo_fault = udv_fault,
78 	.pgo_flush = udv_flush,
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 	/* before we do anything, ensure this device supports mmap */
101 	mapfn = cdevsw[major(device)].d_mmap;
102 	if (mapfn == NULL ||
103 	    mapfn == (paddr_t (*)(dev_t, off_t, int)) enodev ||
104 	    mapfn == (paddr_t (*)(dev_t, off_t, int)) nullop)
105 		return(NULL);
106 
107 	/* Negative offsets on the object are not allowed. */
108 	if (off < 0)
109 		return(NULL);
110 
111 #if NDRM > 0
112 	obj = udv_attach_drm(device, accessprot, off, size);
113 	if (obj)
114 		return(obj);
115 #endif
116 
117 	/*
118 	 * Check that the specified range of the device allows the
119 	 * desired protection.
120 	 *
121 	 * XXX clobbers off and size, but nothing else here needs them.
122 	 */
123 	while (size != 0) {
124 		if ((*mapfn)(device, off, accessprot) == -1)
125 			return (NULL);
126 		off += PAGE_SIZE; size -= PAGE_SIZE;
127 	}
128 
129 	/* keep looping until we get it */
130 	for (;;) {
131 		/* first, attempt to find it on the main list */
132 		mtx_enter(&udv_lock);
133 		LIST_FOREACH(lcv, &udv_list, u_list) {
134 			if (device == lcv->u_device)
135 				break;
136 		}
137 
138 		/* got it on main list.  put a hold on it and unlock udv_lock. */
139 		if (lcv) {
140 			/*
141 			 * if someone else has a hold on it, sleep and start
142 			 * over again. Else, we need take HOLD flag so we
143 			 * don't have to re-order locking here.
144 			 */
145 			if (lcv->u_flags & UVM_DEVICE_HOLD) {
146 				lcv->u_flags |= UVM_DEVICE_WANTED;
147 				msleep_nsec(lcv, &udv_lock, PVM | PNORELOCK,
148 				    "udv_attach", INFSLP);
149 				continue;
150 			}
151 
152 			/* we are now holding it */
153 			lcv->u_flags |= UVM_DEVICE_HOLD;
154 			mtx_leave(&udv_lock);
155 
156 			/* bump reference count, unhold, return. */
157 			lcv->u_obj.uo_refs++;
158 
159 			mtx_enter(&udv_lock);
160 			if (lcv->u_flags & UVM_DEVICE_WANTED)
161 				wakeup(lcv);
162 			lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
163 			mtx_leave(&udv_lock);
164 			return(&lcv->u_obj);
165 		}
166 
167 		/* did not find it on main list.   need to malloc a new one. */
168 		mtx_leave(&udv_lock);
169 		/* NOTE: we could sleep in the following malloc() */
170 		udv = malloc(sizeof(*udv), M_TEMP, M_WAITOK);
171 		mtx_enter(&udv_lock);
172 
173 		/*
174 		 * now we have to double check to make sure no one added it
175 		 * to the list while we were sleeping...
176 		 */
177 		LIST_FOREACH(lcv, &udv_list, u_list) {
178 			if (device == lcv->u_device)
179 				break;
180 		}
181 
182 		/*
183 		 * did we lose a race to someone else?
184 		 * free our memory and retry.
185 		 */
186 		if (lcv) {
187 			mtx_leave(&udv_lock);
188 			free(udv, M_TEMP, sizeof(*udv));
189 			continue;
190 		}
191 
192 		/*
193 		 * we have it!   init the data structures, add to list
194 		 * and return.
195 		 */
196 		uvm_objinit(&udv->u_obj, &uvm_deviceops, 1);
197 		udv->u_flags = 0;
198 		udv->u_device = device;
199 		LIST_INSERT_HEAD(&udv_list, udv, u_list);
200 		mtx_leave(&udv_lock);
201 		return(&udv->u_obj);
202 	}
203 	/*NOTREACHED*/
204 }
205 
206 /*
207  * udv_reference
208  *
209  * add a reference to a VM object.   Note that the reference count must
210  * already be one (the passed in reference) so there is no chance of the
211  * udv being released or locked out here.
212  */
213 static void
214 udv_reference(struct uvm_object *uobj)
215 {
216 	KERNEL_ASSERT_LOCKED();
217 	uobj->uo_refs++;
218 }
219 
220 /*
221  * udv_detach
222  *
223  * remove a reference to a VM object.
224  */
225 static void
226 udv_detach(struct uvm_object *uobj)
227 {
228 	struct uvm_device *udv = (struct uvm_device *)uobj;
229 
230 	KERNEL_ASSERT_LOCKED();
231 
232 	/* loop until done */
233 again:
234 	if (uobj->uo_refs > 1) {
235 		uobj->uo_refs--;
236 		return;
237 	}
238 	KASSERT(uobj->uo_npages == 0 && RBT_EMPTY(uvm_objtree, &uobj->memt));
239 
240 	/* is it being held?   if so, wait until others are done. */
241 	mtx_enter(&udv_lock);
242 	if (udv->u_flags & UVM_DEVICE_HOLD) {
243 		udv->u_flags |= UVM_DEVICE_WANTED;
244 		/*
245 		 * lock interleaving. -- this is ok in this case since the
246 		 * locks are both IPL_NONE
247 		 */
248 		msleep_nsec(udv, &udv_lock, PVM | PNORELOCK, "udv_detach",
249 		    INFSLP);
250 		goto again;
251 	}
252 
253 	/* got it!   nuke it now. */
254 	LIST_REMOVE(udv, u_list);
255 	if (udv->u_flags & UVM_DEVICE_WANTED)
256 		wakeup(udv);
257 	mtx_leave(&udv_lock);
258 	free(udv, M_TEMP, sizeof(*udv));
259 }
260 
261 
262 /*
263  * udv_flush
264  *
265  * flush pages out of a uvm object.   a no-op for devices.
266  */
267 static boolean_t
268 udv_flush(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
269 {
270 
271 	return(TRUE);
272 }
273 
274 /*
275  * udv_fault: non-standard fault routine for device "pages"
276  *
277  * => rather than having a "get" function, we have a fault routine
278  *	since we don't return vm_pages we need full control over the
279  *	pmap_enter map in
280  * => on return, we unlock all fault data structures
281  * => flags: PGO_ALLPAGES: get all of the pages
282  *	     PGO_LOCKED: fault data structures are locked
283  *    XXX: currently PGO_LOCKED is always required ... consider removing
284  *	it as a flag
285  * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
286  */
287 static int
288 udv_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, vm_page_t *pps, int npages,
289     int centeridx, vm_fault_t fault_type, vm_prot_t access_type, int flags)
290 {
291 	struct vm_map_entry *entry = ufi->entry;
292 	struct uvm_object *uobj = entry->object.uvm_obj;
293 	struct uvm_device *udv = (struct uvm_device *)uobj;
294 	vaddr_t curr_va;
295 	off_t curr_offset;
296 	paddr_t paddr;
297 	int lcv, retval;
298 	dev_t device;
299 	paddr_t (*mapfn)(dev_t, off_t, int);
300 	vm_prot_t mapprot;
301 
302 	KERNEL_ASSERT_LOCKED();
303 
304 	/*
305 	 * we do not allow device mappings to be mapped copy-on-write
306 	 * so we kill any attempt to do so here.
307 	 */
308 	if (UVM_ET_ISCOPYONWRITE(entry)) {
309 		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
310 		return(VM_PAGER_ERROR);
311 	}
312 
313 	/* get device map function. */
314 	device = udv->u_device;
315 	mapfn = cdevsw[major(device)].d_mmap;
316 
317 	/*
318 	 * now we must determine the offset in udv to use and the VA to
319 	 * use for pmap_enter.  note that we always use orig_map's pmap
320 	 * for pmap_enter (even if we have a submap).   since virtual
321 	 * addresses in a submap must match the main map, this is ok.
322 	 */
323 	/* udv offset = (offset from start of entry) + entry's offset */
324 	curr_offset = entry->offset + (vaddr - entry->start);
325 	/* pmap va = vaddr (virtual address of pps[0]) */
326 	curr_va = vaddr;
327 
328 	/* loop over the page range entering in as needed */
329 	retval = VM_PAGER_OK;
330 	for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
331 	    curr_va += PAGE_SIZE) {
332 		if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
333 			continue;
334 
335 		if (pps[lcv] == PGO_DONTCARE)
336 			continue;
337 
338 		paddr = (*mapfn)(device, curr_offset, access_type);
339 		if (paddr == -1) {
340 			retval = VM_PAGER_ERROR;
341 			break;
342 		}
343 		mapprot = ufi->entry->protection;
344 		if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr,
345 		    mapprot, PMAP_CANFAIL | mapprot) != 0) {
346 			/*
347 			 * pmap_enter() didn't have the resource to
348 			 * enter this mapping.  Unlock everything,
349 			 * wait for the pagedaemon to free up some
350 			 * pages, and then tell uvm_fault() to start
351 			 * the fault again.
352 			 *
353 			 * XXX Needs some rethinking for the PGO_ALLPAGES
354 			 * XXX case.
355 			 */
356 			uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
357 			    uobj);
358 
359 			/* sync what we have so far */
360 			pmap_update(ufi->orig_map->pmap);
361 			uvm_wait("udv_fault");
362 			return (VM_PAGER_REFAULT);
363 		}
364 	}
365 
366 	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
367 	pmap_update(ufi->orig_map->pmap);
368 	return (retval);
369 }
370