xref: /freebsd/sys/vm/device_pager.c (revision fb919e4d5a2c1baca52ac70d1064f140fffdda71)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
195929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
3826f9a767SRodney W. Grimes  *	@(#)device_pager.c	8.1 (Berkeley) 6/11/93
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44df8bae1dSRodney W. Grimes #include <sys/conf.h>
45fb919e4dSMark Murray #include <sys/lock.h>
46fb919e4dSMark Murray #include <sys/mutex.h>
47df8bae1dSRodney W. Grimes #include <sys/mman.h>
48a9fa2c05SAlfred Perlstein #include <sys/sx.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <vm/vm.h>
51efeaf95aSDavid Greenman #include <vm/vm_object.h>
52df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
5324a1cce3SDavid Greenman #include <vm/vm_pager.h>
5424166bb8SDavid Greenman #include <vm/vm_zone.h>
55df8bae1dSRodney W. Grimes 
56f708ef1bSPoul-Henning Kamp static void dev_pager_init __P((void));
576cde7a16SDavid Greenman static vm_object_t dev_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t,
58f708ef1bSPoul-Henning Kamp 		vm_ooffset_t));
59f708ef1bSPoul-Henning Kamp static void dev_pager_dealloc __P((vm_object_t));
60f708ef1bSPoul-Henning Kamp static int dev_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
61e4542174SMatthew Dillon static void dev_pager_putpages __P((vm_object_t, vm_page_t *, int,
62f708ef1bSPoul-Henning Kamp 		boolean_t, int *));
63f708ef1bSPoul-Henning Kamp static boolean_t dev_pager_haspage __P((vm_object_t, vm_pindex_t, int *,
64f708ef1bSPoul-Henning Kamp 		int *));
65f708ef1bSPoul-Henning Kamp 
66f708ef1bSPoul-Henning Kamp /* list of device pager objects */
67f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list;
68a9fa2c05SAlfred Perlstein /* protect against object creation */
69a9fa2c05SAlfred Perlstein static struct sx dev_pager_sx;
70a9fa2c05SAlfred Perlstein /* protect list manipulation */
71a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx;
72a9fa2c05SAlfred Perlstein 
73f708ef1bSPoul-Henning Kamp 
7424166bb8SDavid Greenman static vm_zone_t fakepg_zone;
7524166bb8SDavid Greenman static struct vm_zone fakepg_zone_store;
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes static vm_page_t dev_pager_getfake __P((vm_offset_t));
78df8bae1dSRodney W. Grimes static void dev_pager_putfake __P((vm_page_t));
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes struct pagerops devicepagerops = {
81df8bae1dSRodney W. Grimes 	dev_pager_init,
82df8bae1dSRodney W. Grimes 	dev_pager_alloc,
83df8bae1dSRodney W. Grimes 	dev_pager_dealloc,
8424a1cce3SDavid Greenman 	dev_pager_getpages,
8524a1cce3SDavid Greenman 	dev_pager_putpages,
8624a1cce3SDavid Greenman 	dev_pager_haspage,
8724a1cce3SDavid Greenman 	NULL
88df8bae1dSRodney W. Grimes };
89df8bae1dSRodney W. Grimes 
90f708ef1bSPoul-Henning Kamp static void
91df8bae1dSRodney W. Grimes dev_pager_init()
92df8bae1dSRodney W. Grimes {
9324a1cce3SDavid Greenman 	TAILQ_INIT(&dev_pager_object_list);
94a9fa2c05SAlfred Perlstein 	sx_init(&dev_pager_sx, "dev_pager create");
95a9fa2c05SAlfred Perlstein 	mtx_init(&dev_pager_mtx, "dev_pager list", MTX_DEF);
9624166bb8SDavid Greenman 	fakepg_zone = &fakepg_zone_store;
9724166bb8SDavid Greenman 	zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2);
98df8bae1dSRodney W. Grimes }
99df8bae1dSRodney W. Grimes 
100f708ef1bSPoul-Henning Kamp static vm_object_t
1016cde7a16SDavid Greenman dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
102df8bae1dSRodney W. Grimes {
103df8bae1dSRodney W. Grimes 	dev_t dev;
104cac597e4SBruce Evans 	d_mmap_t *mapfunc;
105df8bae1dSRodney W. Grimes 	vm_object_t object;
1067095ee91SDoug Rabson 	unsigned int npages;
1077095ee91SDoug Rabson 	vm_offset_t off;
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes 	/*
110df8bae1dSRodney W. Grimes 	 * Make sure this device can be mapped.
111df8bae1dSRodney W. Grimes 	 */
112ebb4a317SBruce Evans 	dev = handle;
1134be2eb8cSPoul-Henning Kamp 	mapfunc = devsw(dev)->d_mmap;
114f31d402cSBruce Evans 	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) {
115f31d402cSBruce Evans 		printf("obsolete map function %p\n", (void *)mapfunc);
116df8bae1dSRodney W. Grimes 		return (NULL);
117f31d402cSBruce Evans 	}
118df8bae1dSRodney W. Grimes 
119df8bae1dSRodney W. Grimes 	/*
120df8bae1dSRodney W. Grimes 	 * Offset should be page aligned.
121df8bae1dSRodney W. Grimes 	 */
122aa8de40aSPoul-Henning Kamp 	if (foff & PAGE_MASK)
123df8bae1dSRodney W. Grimes 		return (NULL);
124df8bae1dSRodney W. Grimes 
1256cde7a16SDavid Greenman 	size = round_page(size);
1266cde7a16SDavid Greenman 
127df8bae1dSRodney W. Grimes 	/*
1280d94caffSDavid Greenman 	 * Check that the specified range of the device allows the desired
1290d94caffSDavid Greenman 	 * protection.
130df8bae1dSRodney W. Grimes 	 *
131df8bae1dSRodney W. Grimes 	 * XXX assumes VM_PROT_* == PROT_*
132df8bae1dSRodney W. Grimes 	 */
1336cde7a16SDavid Greenman 	npages = OFF_TO_IDX(size);
134df8bae1dSRodney W. Grimes 	for (off = foff; npages--; off += PAGE_SIZE)
135df8bae1dSRodney W. Grimes 		if ((*mapfunc) (dev, off, (int) prot) == -1)
136df8bae1dSRodney W. Grimes 			return (NULL);
137df8bae1dSRodney W. Grimes 
138df8bae1dSRodney W. Grimes 	/*
139956f3135SPhilippe Charnier 	 * Lock to prevent object creation race condition.
14024a1cce3SDavid Greenman 	 */
141a9fa2c05SAlfred Perlstein 	sx_xlock(&dev_pager_sx);
14224a1cce3SDavid Greenman 
14324a1cce3SDavid Greenman 	/*
144df8bae1dSRodney W. Grimes 	 * Look up pager, creating as necessary.
145df8bae1dSRodney W. Grimes 	 */
14624a1cce3SDavid Greenman 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
14724a1cce3SDavid Greenman 	if (object == NULL) {
148df8bae1dSRodney W. Grimes 		/*
149df8bae1dSRodney W. Grimes 		 * Allocate object and associate it with the pager.
150df8bae1dSRodney W. Grimes 		 */
151a316d390SJohn Dyson 		object = vm_object_allocate(OBJT_DEVICE,
1526cde7a16SDavid Greenman 			OFF_TO_IDX(foff + size));
15324a1cce3SDavid Greenman 		object->handle = handle;
15424a1cce3SDavid Greenman 		TAILQ_INIT(&object->un_pager.devp.devp_pglist);
155a9fa2c05SAlfred Perlstein 		mtx_lock(&dev_pager_mtx);
15624a1cce3SDavid Greenman 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
157a9fa2c05SAlfred Perlstein 		mtx_unlock(&dev_pager_mtx);
158df8bae1dSRodney W. Grimes 	} else {
159df8bae1dSRodney W. Grimes 		/*
1607fb0c17eSDavid Greenman 		 * Gain a reference to the object.
161df8bae1dSRodney W. Grimes 		 */
16224a1cce3SDavid Greenman 		vm_object_reference(object);
1636cde7a16SDavid Greenman 		if (OFF_TO_IDX(foff + size) > object->size)
1646cde7a16SDavid Greenman 			object->size = OFF_TO_IDX(foff + size);
165df8bae1dSRodney W. Grimes 	}
166df8bae1dSRodney W. Grimes 
167a9fa2c05SAlfred Perlstein 	sx_xunlock(&dev_pager_sx);
16824a1cce3SDavid Greenman 
16924a1cce3SDavid Greenman 	return (object);
17024a1cce3SDavid Greenman }
17124a1cce3SDavid Greenman 
172f708ef1bSPoul-Henning Kamp static void
17324a1cce3SDavid Greenman dev_pager_dealloc(object)
174df8bae1dSRodney W. Grimes 	vm_object_t object;
17524a1cce3SDavid Greenman {
176df8bae1dSRodney W. Grimes 	vm_page_t m;
177df8bae1dSRodney W. Grimes 
178a9fa2c05SAlfred Perlstein 	mtx_lock(&dev_pager_mtx);
17924a1cce3SDavid Greenman 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
180a9fa2c05SAlfred Perlstein 	mtx_unlock(&dev_pager_mtx);
181df8bae1dSRodney W. Grimes 	/*
182df8bae1dSRodney W. Grimes 	 * Free up our fake pages.
183df8bae1dSRodney W. Grimes 	 */
184b18bfc3dSJohn Dyson 	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
18524a1cce3SDavid Greenman 		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
186df8bae1dSRodney W. Grimes 		dev_pager_putfake(m);
187df8bae1dSRodney W. Grimes 	}
188df8bae1dSRodney W. Grimes }
189df8bae1dSRodney W. Grimes 
190f708ef1bSPoul-Henning Kamp static int
19124a1cce3SDavid Greenman dev_pager_getpages(object, m, count, reqpage)
19224a1cce3SDavid Greenman 	vm_object_t object;
19324a1cce3SDavid Greenman 	vm_page_t *m;
19424a1cce3SDavid Greenman 	int count;
19524a1cce3SDavid Greenman 	int reqpage;
196df8bae1dSRodney W. Grimes {
197a316d390SJohn Dyson 	vm_offset_t offset;
198a316d390SJohn Dyson 	vm_offset_t paddr;
199df8bae1dSRodney W. Grimes 	vm_page_t page;
200df8bae1dSRodney W. Grimes 	dev_t dev;
20124a1cce3SDavid Greenman 	int i, s;
202cac597e4SBruce Evans 	d_mmap_t *mapfunc;
203cac597e4SBruce Evans 	int prot;
204df8bae1dSRodney W. Grimes 
205ebb4a317SBruce Evans 	dev = object->handle;
2061c7c3c6aSMatthew Dillon 	offset = m[reqpage]->pindex;
207df8bae1dSRodney W. Grimes 	prot = PROT_READ;	/* XXX should pass in? */
2084be2eb8cSPoul-Henning Kamp 	mapfunc = devsw(dev)->d_mmap;
20926f9a767SRodney W. Grimes 
210f31d402cSBruce Evans 	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop)
211df8bae1dSRodney W. Grimes 		panic("dev_pager_getpage: no map function");
21226f9a767SRodney W. Grimes 
213bfbb9ce6SPoul-Henning Kamp 	paddr = pmap_phys_address((*mapfunc) (dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
2145526d2d9SEivind Eklund 	KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
215df8bae1dSRodney W. Grimes 	/*
21624a1cce3SDavid Greenman 	 * Replace the passed in reqpage page with our own fake page and free up the
21724a1cce3SDavid Greenman 	 * all of the original pages.
218df8bae1dSRodney W. Grimes 	 */
219df8bae1dSRodney W. Grimes 	page = dev_pager_getfake(paddr);
22024a1cce3SDavid Greenman 	TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
22124a1cce3SDavid Greenman 	for (i = 0; i < count; i++) {
22224a1cce3SDavid Greenman 		vm_page_free(m[i]);
22324a1cce3SDavid Greenman 	}
22426f9a767SRodney W. Grimes 	s = splhigh();
22526f9a767SRodney W. Grimes 	vm_page_insert(page, object, offset);
22626f9a767SRodney W. Grimes 	splx(s);
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes 	return (VM_PAGER_OK);
229df8bae1dSRodney W. Grimes }
230df8bae1dSRodney W. Grimes 
231e4542174SMatthew Dillon static void
23224a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals)
23324a1cce3SDavid Greenman 	vm_object_t object;
23424a1cce3SDavid Greenman 	vm_page_t *m;
23524a1cce3SDavid Greenman 	int count;
236df8bae1dSRodney W. Grimes 	boolean_t sync;
23724a1cce3SDavid Greenman 	int *rtvals;
238df8bae1dSRodney W. Grimes {
239df8bae1dSRodney W. Grimes 	panic("dev_pager_putpage called");
240df8bae1dSRodney W. Grimes }
241df8bae1dSRodney W. Grimes 
242f708ef1bSPoul-Henning Kamp static boolean_t
243a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after)
24424a1cce3SDavid Greenman 	vm_object_t object;
245a316d390SJohn Dyson 	vm_pindex_t pindex;
24624a1cce3SDavid Greenman 	int *before;
24724a1cce3SDavid Greenman 	int *after;
248df8bae1dSRodney W. Grimes {
24924a1cce3SDavid Greenman 	if (before != NULL)
25024a1cce3SDavid Greenman 		*before = 0;
25124a1cce3SDavid Greenman 	if (after != NULL)
25224a1cce3SDavid Greenman 		*after = 0;
253df8bae1dSRodney W. Grimes 	return (TRUE);
254df8bae1dSRodney W. Grimes }
255df8bae1dSRodney W. Grimes 
256df8bae1dSRodney W. Grimes static vm_page_t
257df8bae1dSRodney W. Grimes dev_pager_getfake(paddr)
258df8bae1dSRodney W. Grimes 	vm_offset_t paddr;
259df8bae1dSRodney W. Grimes {
260df8bae1dSRodney W. Grimes 	vm_page_t m;
261df8bae1dSRodney W. Grimes 
26224166bb8SDavid Greenman 	m = zalloc(fakepg_zone);
26326f9a767SRodney W. Grimes 
2640d94caffSDavid Greenman 	m->flags = PG_BUSY | PG_FICTITIOUS;
2650d94caffSDavid Greenman 	m->valid = VM_PAGE_BITS_ALL;
26624a1cce3SDavid Greenman 	m->dirty = 0;
2670d94caffSDavid Greenman 	m->busy = 0;
268bd7e5f99SJohn Dyson 	m->queue = PQ_NONE;
269bb7db2c0SDavid Greenman 	m->object = NULL;
27026f9a767SRodney W. Grimes 
271df8bae1dSRodney W. Grimes 	m->wire_count = 1;
272c68f9c92SJohn Dyson 	m->hold_count = 0;
27326f9a767SRodney W. Grimes 	m->phys_addr = paddr;
27426f9a767SRodney W. Grimes 
275df8bae1dSRodney W. Grimes 	return (m);
276df8bae1dSRodney W. Grimes }
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes static void
279df8bae1dSRodney W. Grimes dev_pager_putfake(m)
280df8bae1dSRodney W. Grimes 	vm_page_t m;
281df8bae1dSRodney W. Grimes {
282df8bae1dSRodney W. Grimes 	if (!(m->flags & PG_FICTITIOUS))
283df8bae1dSRodney W. Grimes 		panic("dev_pager_putfake: bad page");
28424166bb8SDavid Greenman 	zfree(fakepg_zone, m);
285df8bae1dSRodney W. Grimes }
286