xref: /freebsd/sys/vm/device_pager.c (revision 7253a5ec63021dd98dc70c71aeab0919ddb94931)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
3df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
8df8bae1dSRodney W. Grimes  * Science Department.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
3426f9a767SRodney W. Grimes  *	@(#)device_pager.c	8.1 (Berkeley) 6/11/93
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37874651b1SDavid E. O'Brien #include <sys/cdefs.h>
38874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
39874651b1SDavid E. O'Brien 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42df8bae1dSRodney W. Grimes #include <sys/conf.h>
43fb919e4dSMark Murray #include <sys/lock.h>
440cddd8f0SMatthew Dillon #include <sys/proc.h>
45fb919e4dSMark Murray #include <sys/mutex.h>
46df8bae1dSRodney W. Grimes #include <sys/mman.h>
4789f6b863SAttilio Rao #include <sys/rwlock.h>
48a9fa2c05SAlfred Perlstein #include <sys/sx.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <vm/vm.h>
511c771f92SKonstantin Belousov #include <vm/vm_param.h>
52efeaf95aSDavid Greenman #include <vm/vm_object.h>
53df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
5424a1cce3SDavid Greenman #include <vm/vm_pager.h>
5543f48b65SKonstantin Belousov #include <vm/vm_phys.h>
56670d17b5SJeff Roberson #include <vm/uma.h>
57df8bae1dSRodney W. Grimes 
5811caded3SAlfred Perlstein static void dev_pager_init(void);
5911caded3SAlfred Perlstein static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
603364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *);
6111caded3SAlfred Perlstein static void dev_pager_dealloc(vm_object_t);
6211caded3SAlfred Perlstein static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int);
6311caded3SAlfred Perlstein static void dev_pager_putpages(vm_object_t, vm_page_t *, int,
6411caded3SAlfred Perlstein 		boolean_t, int *);
6511caded3SAlfred Perlstein static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *,
6611caded3SAlfred Perlstein 		int *);
67b7ac5a85SKonstantin Belousov static void dev_pager_free_page(vm_object_t object, vm_page_t m);
68f708ef1bSPoul-Henning Kamp 
69f708ef1bSPoul-Henning Kamp /* list of device pager objects */
70f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list;
71a9fa2c05SAlfred Perlstein /* protect list manipulation */
72a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx;
73a9fa2c05SAlfred Perlstein 
74df8bae1dSRodney W. Grimes struct pagerops devicepagerops = {
754e658600SPoul-Henning Kamp 	.pgo_init =	dev_pager_init,
764e658600SPoul-Henning Kamp 	.pgo_alloc =	dev_pager_alloc,
774e658600SPoul-Henning Kamp 	.pgo_dealloc =	dev_pager_dealloc,
784e658600SPoul-Henning Kamp 	.pgo_getpages =	dev_pager_getpages,
794e658600SPoul-Henning Kamp 	.pgo_putpages =	dev_pager_putpages,
804e658600SPoul-Henning Kamp 	.pgo_haspage =	dev_pager_haspage,
81df8bae1dSRodney W. Grimes };
82df8bae1dSRodney W. Grimes 
83b7ac5a85SKonstantin Belousov struct pagerops mgtdevicepagerops = {
84b7ac5a85SKonstantin Belousov 	.pgo_alloc =	dev_pager_alloc,
85b7ac5a85SKonstantin Belousov 	.pgo_dealloc =	dev_pager_dealloc,
86b7ac5a85SKonstantin Belousov 	.pgo_getpages =	dev_pager_getpages,
87b7ac5a85SKonstantin Belousov 	.pgo_putpages =	dev_pager_putpages,
88b7ac5a85SKonstantin Belousov 	.pgo_haspage =	dev_pager_haspage,
89b7ac5a85SKonstantin Belousov };
90b7ac5a85SKonstantin Belousov 
91286790a7SKonstantin Belousov static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
92286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred, u_short *color);
93286790a7SKonstantin Belousov static void old_dev_pager_dtor(void *handle);
94286790a7SKonstantin Belousov static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
95286790a7SKonstantin Belousov     int prot, vm_page_t *mres);
96286790a7SKonstantin Belousov 
97286790a7SKonstantin Belousov static struct cdev_pager_ops old_dev_pager_ops = {
98286790a7SKonstantin Belousov 	.cdev_pg_ctor =	old_dev_pager_ctor,
99286790a7SKonstantin Belousov 	.cdev_pg_dtor =	old_dev_pager_dtor,
100286790a7SKonstantin Belousov 	.cdev_pg_fault = old_dev_pager_fault
101286790a7SKonstantin Belousov };
102286790a7SKonstantin Belousov 
103f708ef1bSPoul-Henning Kamp static void
104df8bae1dSRodney W. Grimes dev_pager_init()
105df8bae1dSRodney W. Grimes {
10624a1cce3SDavid Greenman 	TAILQ_INIT(&dev_pager_object_list);
1076008862bSJohn Baldwin 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
108df8bae1dSRodney W. Grimes }
109df8bae1dSRodney W. Grimes 
110286790a7SKonstantin Belousov vm_object_t
111286790a7SKonstantin Belousov cdev_pager_lookup(void *handle)
112df8bae1dSRodney W. Grimes {
113286790a7SKonstantin Belousov 	vm_object_t object;
114286790a7SKonstantin Belousov 
115286790a7SKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
116286790a7SKonstantin Belousov 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
117286790a7SKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
118286790a7SKonstantin Belousov 	return (object);
119286790a7SKonstantin Belousov }
120286790a7SKonstantin Belousov 
121286790a7SKonstantin Belousov vm_object_t
122286790a7SKonstantin Belousov cdev_pager_allocate(void *handle, enum obj_type tp, struct cdev_pager_ops *ops,
123286790a7SKonstantin Belousov     vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred)
124286790a7SKonstantin Belousov {
125deea654eSKonstantin Belousov 	vm_object_t object, object1;
1262f7af3dbSAlan Cox 	vm_pindex_t pindex;
127286790a7SKonstantin Belousov 	u_short color;
128286790a7SKonstantin Belousov 
129b7ac5a85SKonstantin Belousov 	if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE)
130286790a7SKonstantin Belousov 		return (NULL);
131df8bae1dSRodney W. Grimes 
132df8bae1dSRodney W. Grimes 	/*
133df8bae1dSRodney W. Grimes 	 * Offset should be page aligned.
134df8bae1dSRodney W. Grimes 	 */
135aa8de40aSPoul-Henning Kamp 	if (foff & PAGE_MASK)
136df8bae1dSRodney W. Grimes 		return (NULL);
137df8bae1dSRodney W. Grimes 
1386cde7a16SDavid Greenman 	size = round_page(size);
1392f7af3dbSAlan Cox 	pindex = OFF_TO_IDX(foff + size);
1406cde7a16SDavid Greenman 
141286790a7SKonstantin Belousov 	if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)
142c8664f82SAlan Cox 		return (NULL);
143deea654eSKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
14424a1cce3SDavid Greenman 
14524a1cce3SDavid Greenman 	/*
146df8bae1dSRodney W. Grimes 	 * Look up pager, creating as necessary.
147df8bae1dSRodney W. Grimes 	 */
148deea654eSKonstantin Belousov 	object1 = NULL;
14924a1cce3SDavid Greenman 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
15024a1cce3SDavid Greenman 	if (object == NULL) {
151df8bae1dSRodney W. Grimes 		/*
152e46cd413SAlan Cox 		 * Allocate object and associate it with the pager.  Initialize
153e46cd413SAlan Cox 		 * the object's pg_color based upon the physical address of the
154e46cd413SAlan Cox 		 * device's memory.
155df8bae1dSRodney W. Grimes 		 */
156deea654eSKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
157286790a7SKonstantin Belousov 		object1 = vm_object_allocate(tp, pindex);
158e46cd413SAlan Cox 		object1->flags |= OBJ_COLORED;
159286790a7SKonstantin Belousov 		object1->pg_color = color;
160286790a7SKonstantin Belousov 		object1->handle = handle;
161286790a7SKonstantin Belousov 		object1->un_pager.devp.ops = ops;
16243ab9660SKenneth D. Merry 		object1->un_pager.devp.dev = handle;
163339772b0SKonstantin Belousov 		TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
164deea654eSKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
165deea654eSKonstantin Belousov 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
166deea654eSKonstantin Belousov 		if (object != NULL) {
167deea654eSKonstantin Belousov 			/*
168deea654eSKonstantin Belousov 			 * We raced with other thread while allocating object.
169deea654eSKonstantin Belousov 			 */
170deea654eSKonstantin Belousov 			if (pindex > object->size)
171deea654eSKonstantin Belousov 				object->size = pindex;
1728add0cedSJohn Baldwin 			KASSERT(object->type == tp,
1738add0cedSJohn Baldwin 		    ("Inconsistent device pager type %p %d", object, tp));
174deea654eSKonstantin Belousov 		} else {
175deea654eSKonstantin Belousov 			object = object1;
176deea654eSKonstantin Belousov 			object1 = NULL;
17724a1cce3SDavid Greenman 			object->handle = handle;
178deea654eSKonstantin Belousov 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
179deea654eSKonstantin Belousov 			    pager_object_list);
180deea654eSKonstantin Belousov 		}
181df8bae1dSRodney W. Grimes 	} else {
1822f7af3dbSAlan Cox 		if (pindex > object->size)
1832f7af3dbSAlan Cox 			object->size = pindex;
1848add0cedSJohn Baldwin 		KASSERT(object->type == tp,
1858add0cedSJohn Baldwin 		    ("Inconsistent device pager type %p %d", object, tp));
186df8bae1dSRodney W. Grimes 	}
187deea654eSKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
188339772b0SKonstantin Belousov 	if (object1 != NULL) {
189339772b0SKonstantin Belousov 		object1->handle = object1;
190339772b0SKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
191339772b0SKonstantin Belousov 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
192339772b0SKonstantin Belousov 		    pager_object_list);
193339772b0SKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
194deea654eSKonstantin Belousov 		vm_object_deallocate(object1);
195339772b0SKonstantin Belousov 	}
19624a1cce3SDavid Greenman 	return (object);
19724a1cce3SDavid Greenman }
19824a1cce3SDavid Greenman 
199286790a7SKonstantin Belousov static vm_object_t
200286790a7SKonstantin Belousov dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
201286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred)
202286790a7SKonstantin Belousov {
203286790a7SKonstantin Belousov 
204286790a7SKonstantin Belousov 	return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
205286790a7SKonstantin Belousov 	    size, prot, foff, cred));
206286790a7SKonstantin Belousov }
207286790a7SKonstantin Belousov 
208286790a7SKonstantin Belousov void
209286790a7SKonstantin Belousov cdev_pager_free_page(vm_object_t object, vm_page_t m)
210286790a7SKonstantin Belousov {
211286790a7SKonstantin Belousov 
21289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
213b7ac5a85SKonstantin Belousov 	if (object->type == OBJT_MGTDEVICE) {
214b7ac5a85SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m));
215b7ac5a85SKonstantin Belousov 		pmap_remove_all(m);
216b7ac5a85SKonstantin Belousov 		vm_page_lock(m);
217b7ac5a85SKonstantin Belousov 		vm_page_remove(m);
218b7ac5a85SKonstantin Belousov 		vm_page_unlock(m);
219b7ac5a85SKonstantin Belousov 	} else if (object->type == OBJT_DEVICE)
220b7ac5a85SKonstantin Belousov 		dev_pager_free_page(object, m);
221b7ac5a85SKonstantin Belousov }
222b7ac5a85SKonstantin Belousov 
223b7ac5a85SKonstantin Belousov static void
224b7ac5a85SKonstantin Belousov dev_pager_free_page(vm_object_t object, vm_page_t m)
225b7ac5a85SKonstantin Belousov {
226b7ac5a85SKonstantin Belousov 
22789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
228b7ac5a85SKonstantin Belousov 	KASSERT((object->type == OBJT_DEVICE &&
229b7ac5a85SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0),
230b7ac5a85SKonstantin Belousov 	    ("Managed device or page obj %p m %p", object, m));
231c325e866SKonstantin Belousov 	TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
232286790a7SKonstantin Belousov 	vm_page_putfake(m);
233286790a7SKonstantin Belousov }
234286790a7SKonstantin Belousov 
235f708ef1bSPoul-Henning Kamp static void
23624a1cce3SDavid Greenman dev_pager_dealloc(object)
237df8bae1dSRodney W. Grimes 	vm_object_t object;
23824a1cce3SDavid Greenman {
239df8bae1dSRodney W. Grimes 	vm_page_t m;
240df8bae1dSRodney W. Grimes 
24189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
24243ab9660SKenneth D. Merry 	object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev);
243286790a7SKonstantin Belousov 
244a9fa2c05SAlfred Perlstein 	mtx_lock(&dev_pager_mtx);
24524a1cce3SDavid Greenman 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
246a9fa2c05SAlfred Perlstein 	mtx_unlock(&dev_pager_mtx);
24789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
248b7ac5a85SKonstantin Belousov 
249b7ac5a85SKonstantin Belousov 	if (object->type == OBJT_DEVICE) {
250df8bae1dSRodney W. Grimes 		/*
251df8bae1dSRodney W. Grimes 		 * Free up our fake pages.
252df8bae1dSRodney W. Grimes 		 */
253b7ac5a85SKonstantin Belousov 		while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
254b7ac5a85SKonstantin Belousov 		    != NULL)
255b7ac5a85SKonstantin Belousov 			dev_pager_free_page(object, m);
256b7ac5a85SKonstantin Belousov 	}
257df8bae1dSRodney W. Grimes }
258df8bae1dSRodney W. Grimes 
259f708ef1bSPoul-Henning Kamp static int
260286790a7SKonstantin Belousov dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int reqpage)
261df8bae1dSRodney W. Grimes {
262286790a7SKonstantin Belousov 	int error, i;
263df8bae1dSRodney W. Grimes 
26489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
265286790a7SKonstantin Belousov 	error = object->un_pager.devp.ops->cdev_pg_fault(object,
266286790a7SKonstantin Belousov 	    IDX_TO_OFF(ma[reqpage]->pindex), PROT_READ, &ma[reqpage]);
267286790a7SKonstantin Belousov 
26889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
269286790a7SKonstantin Belousov 
270286790a7SKonstantin Belousov 	for (i = 0; i < count; i++) {
271286790a7SKonstantin Belousov 		if (i != reqpage) {
272286790a7SKonstantin Belousov 			vm_page_lock(ma[i]);
273286790a7SKonstantin Belousov 			vm_page_free(ma[i]);
274286790a7SKonstantin Belousov 			vm_page_unlock(ma[i]);
275286790a7SKonstantin Belousov 		}
276286790a7SKonstantin Belousov 	}
277286790a7SKonstantin Belousov 
278286790a7SKonstantin Belousov 	if (error == VM_PAGER_OK) {
279b7ac5a85SKonstantin Belousov 		KASSERT((object->type == OBJT_DEVICE &&
280b7ac5a85SKonstantin Belousov 		     (ma[reqpage]->oflags & VPO_UNMANAGED) != 0) ||
281b7ac5a85SKonstantin Belousov 		    (object->type == OBJT_MGTDEVICE &&
282b7ac5a85SKonstantin Belousov 		     (ma[reqpage]->oflags & VPO_UNMANAGED) == 0),
283b7ac5a85SKonstantin Belousov 		    ("Wrong page type %p %p", ma[reqpage], object));
284b7ac5a85SKonstantin Belousov 		if (object->type == OBJT_DEVICE) {
285286790a7SKonstantin Belousov 			TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
286c325e866SKonstantin Belousov 			    ma[reqpage], plinks.q);
287286790a7SKonstantin Belousov 		}
288b7ac5a85SKonstantin Belousov 	}
289286790a7SKonstantin Belousov 
290286790a7SKonstantin Belousov 	return (error);
291286790a7SKonstantin Belousov }
292286790a7SKonstantin Belousov 
293286790a7SKonstantin Belousov static int
294286790a7SKonstantin Belousov old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
295286790a7SKonstantin Belousov     vm_page_t *mres)
296286790a7SKonstantin Belousov {
297286790a7SKonstantin Belousov 	vm_pindex_t pidx;
298286790a7SKonstantin Belousov 	vm_paddr_t paddr;
299286790a7SKonstantin Belousov 	vm_page_t m_paddr, page;
300286790a7SKonstantin Belousov 	struct cdev *dev;
301286790a7SKonstantin Belousov 	struct cdevsw *csw;
302286790a7SKonstantin Belousov 	struct file *fpop;
303286790a7SKonstantin Belousov 	struct thread *td;
304286790a7SKonstantin Belousov 	vm_memattr_t memattr;
305286790a7SKonstantin Belousov 	int ref, ret;
306286790a7SKonstantin Belousov 
307286790a7SKonstantin Belousov 	pidx = OFF_TO_IDX(offset);
3083153e878SAlan Cox 	memattr = object->memattr;
309286790a7SKonstantin Belousov 
31089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
311286790a7SKonstantin Belousov 
312286790a7SKonstantin Belousov 	dev = object->handle;
3133979450bSKonstantin Belousov 	csw = dev_refthread(dev, &ref);
31491a1929fSAttilio Rao 	if (csw == NULL) {
31589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
31691a1929fSAttilio Rao 		return (VM_PAGER_FAIL);
31791a1929fSAttilio Rao 	}
3187818e0a5SKonstantin Belousov 	td = curthread;
3197818e0a5SKonstantin Belousov 	fpop = td->td_fpop;
3207818e0a5SKonstantin Belousov 	td->td_fpop = NULL;
321286790a7SKonstantin Belousov 	ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
3227818e0a5SKonstantin Belousov 	td->td_fpop = fpop;
3233979450bSKonstantin Belousov 	dev_relthread(dev, ref);
324286790a7SKonstantin Belousov 	if (ret != 0) {
325286790a7SKonstantin Belousov 		printf(
326286790a7SKonstantin Belousov 	    "WARNING: dev_pager_getpage: map function returns error %d", ret);
32789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
328286790a7SKonstantin Belousov 		return (VM_PAGER_FAIL);
329286790a7SKonstantin Belousov 	}
330286790a7SKonstantin Belousov 
3313153e878SAlan Cox 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
3323153e878SAlan Cox 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
3333153e878SAlan Cox 	    pmap_page_get_memattr(m_paddr) != memattr) {
3343153e878SAlan Cox 		memattr = pmap_page_get_memattr(m_paddr);
3353153e878SAlan Cox 		printf(
3363153e878SAlan Cox 	    "WARNING: A device driver has set \"memattr\" inconsistently.\n");
3373153e878SAlan Cox 	}
338286790a7SKonstantin Belousov 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
33992bab635SDoug Rabson 		/*
340286790a7SKonstantin Belousov 		 * If the passed in result page is a fake page, update it with
34192bab635SDoug Rabson 		 * the new physical address.
34292bab635SDoug Rabson 		 */
343286790a7SKonstantin Belousov 		page = *mres;
34489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
34510cf2560SAlan Cox 		vm_page_updatefake(page, paddr, memattr);
34692bab635SDoug Rabson 	} else {
347df8bae1dSRodney W. Grimes 		/*
34807159f9cSMaxime Henrion 		 * Replace the passed in reqpage page with our own fake page and
34907159f9cSMaxime Henrion 		 * free up the all of the original pages.
350df8bae1dSRodney W. Grimes 		 */
35110cf2560SAlan Cox 		page = vm_page_getfake(paddr, memattr);
35289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
353e946b949SAttilio Rao 		if (vm_page_replace(page, object, (*mres)->pindex) != *mres)
354e946b949SAttilio Rao 			panic("old_dev_pager_fault: invalid page replacement");
355286790a7SKonstantin Belousov 		vm_page_lock(*mres);
356286790a7SKonstantin Belousov 		vm_page_free(*mres);
357286790a7SKonstantin Belousov 		vm_page_unlock(*mres);
358286790a7SKonstantin Belousov 		*mres = page;
35992bab635SDoug Rabson 	}
360ce1b857fSAlan Cox 	page->valid = VM_PAGE_BITS_ALL;
361df8bae1dSRodney W. Grimes 	return (VM_PAGER_OK);
362df8bae1dSRodney W. Grimes }
363df8bae1dSRodney W. Grimes 
364e4542174SMatthew Dillon static void
36524a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals)
36624a1cce3SDavid Greenman 	vm_object_t object;
36724a1cce3SDavid Greenman 	vm_page_t *m;
36824a1cce3SDavid Greenman 	int count;
369df8bae1dSRodney W. Grimes 	boolean_t sync;
37024a1cce3SDavid Greenman 	int *rtvals;
371df8bae1dSRodney W. Grimes {
372286790a7SKonstantin Belousov 
373df8bae1dSRodney W. Grimes 	panic("dev_pager_putpage called");
374df8bae1dSRodney W. Grimes }
375df8bae1dSRodney W. Grimes 
376f708ef1bSPoul-Henning Kamp static boolean_t
377a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after)
37824a1cce3SDavid Greenman 	vm_object_t object;
379a316d390SJohn Dyson 	vm_pindex_t pindex;
38024a1cce3SDavid Greenman 	int *before;
38124a1cce3SDavid Greenman 	int *after;
382df8bae1dSRodney W. Grimes {
38324a1cce3SDavid Greenman 	if (before != NULL)
38424a1cce3SDavid Greenman 		*before = 0;
38524a1cce3SDavid Greenman 	if (after != NULL)
38624a1cce3SDavid Greenman 		*after = 0;
387df8bae1dSRodney W. Grimes 	return (TRUE);
388df8bae1dSRodney W. Grimes }
389286790a7SKonstantin Belousov 
390286790a7SKonstantin Belousov static int
391286790a7SKonstantin Belousov old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
392286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred, u_short *color)
393286790a7SKonstantin Belousov {
394286790a7SKonstantin Belousov 	struct cdev *dev;
395286790a7SKonstantin Belousov 	struct cdevsw *csw;
396286790a7SKonstantin Belousov 	vm_memattr_t dummy;
397286790a7SKonstantin Belousov 	vm_ooffset_t off;
398286790a7SKonstantin Belousov 	vm_paddr_t paddr;
399286790a7SKonstantin Belousov 	unsigned int npages;
400286790a7SKonstantin Belousov 	int ref;
401286790a7SKonstantin Belousov 
402286790a7SKonstantin Belousov 	/*
403286790a7SKonstantin Belousov 	 * Make sure this device can be mapped.
404286790a7SKonstantin Belousov 	 */
405286790a7SKonstantin Belousov 	dev = handle;
406286790a7SKonstantin Belousov 	csw = dev_refthread(dev, &ref);
407286790a7SKonstantin Belousov 	if (csw == NULL)
408286790a7SKonstantin Belousov 		return (ENXIO);
409286790a7SKonstantin Belousov 
410286790a7SKonstantin Belousov 	/*
411286790a7SKonstantin Belousov 	 * Check that the specified range of the device allows the desired
412286790a7SKonstantin Belousov 	 * protection.
413286790a7SKonstantin Belousov 	 *
414286790a7SKonstantin Belousov 	 * XXX assumes VM_PROT_* == PROT_*
415286790a7SKonstantin Belousov 	 */
416286790a7SKonstantin Belousov 	npages = OFF_TO_IDX(size);
417*7253a5ecSKonstantin Belousov 	paddr = 0; /* Make paddr initialized for the case of size == 0. */
418286790a7SKonstantin Belousov 	for (off = foff; npages--; off += PAGE_SIZE) {
419286790a7SKonstantin Belousov 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
420286790a7SKonstantin Belousov 			dev_relthread(dev, ref);
421286790a7SKonstantin Belousov 			return (EINVAL);
422286790a7SKonstantin Belousov 		}
423286790a7SKonstantin Belousov 	}
424286790a7SKonstantin Belousov 
425286790a7SKonstantin Belousov 	dev_ref(dev);
426286790a7SKonstantin Belousov 	dev_relthread(dev, ref);
427286790a7SKonstantin Belousov 	*color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
428286790a7SKonstantin Belousov 	return (0);
429286790a7SKonstantin Belousov }
430286790a7SKonstantin Belousov 
431286790a7SKonstantin Belousov static void
432286790a7SKonstantin Belousov old_dev_pager_dtor(void *handle)
433286790a7SKonstantin Belousov {
434286790a7SKonstantin Belousov 
435286790a7SKonstantin Belousov 	dev_rel(handle);
436286790a7SKonstantin Belousov }
437