xref: /freebsd/sys/vm/device_pager.c (revision deea654ebfd5fdbb803ccda0077aeb64d9cf3f54)
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>
47a9fa2c05SAlfred Perlstein #include <sys/sx.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <vm/vm.h>
50efeaf95aSDavid Greenman #include <vm/vm_object.h>
51df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
5224a1cce3SDavid Greenman #include <vm/vm_pager.h>
53670d17b5SJeff Roberson #include <vm/uma.h>
54df8bae1dSRodney W. Grimes 
5511caded3SAlfred Perlstein static void dev_pager_init(void);
5611caded3SAlfred Perlstein static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
5711caded3SAlfred Perlstein 		vm_ooffset_t);
5811caded3SAlfred Perlstein static void dev_pager_dealloc(vm_object_t);
5911caded3SAlfred Perlstein static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int);
6011caded3SAlfred Perlstein static void dev_pager_putpages(vm_object_t, vm_page_t *, int,
6111caded3SAlfred Perlstein 		boolean_t, int *);
6211caded3SAlfred Perlstein static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *,
6311caded3SAlfred Perlstein 		int *);
64f708ef1bSPoul-Henning Kamp 
65f708ef1bSPoul-Henning Kamp /* list of device pager objects */
66f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list;
67a9fa2c05SAlfred Perlstein /* protect list manipulation */
68a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx;
69a9fa2c05SAlfred Perlstein 
70f708ef1bSPoul-Henning Kamp 
71670d17b5SJeff Roberson static uma_zone_t fakepg_zone;
72df8bae1dSRodney W. Grimes 
73227f9a1cSJake Burkholder static vm_page_t dev_pager_getfake(vm_paddr_t);
7411caded3SAlfred Perlstein static void dev_pager_putfake(vm_page_t);
7592bab635SDoug Rabson static void dev_pager_updatefake(vm_page_t, vm_paddr_t);
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes struct pagerops devicepagerops = {
784e658600SPoul-Henning Kamp 	.pgo_init =	dev_pager_init,
794e658600SPoul-Henning Kamp 	.pgo_alloc =	dev_pager_alloc,
804e658600SPoul-Henning Kamp 	.pgo_dealloc =	dev_pager_dealloc,
814e658600SPoul-Henning Kamp 	.pgo_getpages =	dev_pager_getpages,
824e658600SPoul-Henning Kamp 	.pgo_putpages =	dev_pager_putpages,
834e658600SPoul-Henning Kamp 	.pgo_haspage =	dev_pager_haspage,
84df8bae1dSRodney W. Grimes };
85df8bae1dSRodney W. Grimes 
86f708ef1bSPoul-Henning Kamp static void
87df8bae1dSRodney W. Grimes dev_pager_init()
88df8bae1dSRodney W. Grimes {
8924a1cce3SDavid Greenman 	TAILQ_INIT(&dev_pager_object_list);
906008862bSJohn Baldwin 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
91670d17b5SJeff Roberson 	fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page),
92f3c625e4SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
93f3c625e4SJeff Roberson 	    UMA_ZONE_NOFREE|UMA_ZONE_VM);
94df8bae1dSRodney W. Grimes }
95df8bae1dSRodney W. Grimes 
96c8664f82SAlan Cox /*
97c8664f82SAlan Cox  * MPSAFE
98c8664f82SAlan Cox  */
99f708ef1bSPoul-Henning Kamp static vm_object_t
1006cde7a16SDavid Greenman dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
101df8bae1dSRodney W. Grimes {
10289c9c53dSPoul-Henning Kamp 	struct cdev *dev;
103deea654eSKonstantin Belousov 	vm_object_t object, object1;
1042f7af3dbSAlan Cox 	vm_pindex_t pindex;
1057095ee91SDoug Rabson 	unsigned int npages;
106227f9a1cSJake Burkholder 	vm_paddr_t paddr;
107227f9a1cSJake Burkholder 	vm_offset_t off;
108751fdd08SPoul-Henning Kamp 	struct cdevsw *csw;
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes 	/*
111df8bae1dSRodney W. Grimes 	 * Offset should be page aligned.
112df8bae1dSRodney W. Grimes 	 */
113aa8de40aSPoul-Henning Kamp 	if (foff & PAGE_MASK)
114df8bae1dSRodney W. Grimes 		return (NULL);
115df8bae1dSRodney W. Grimes 
1166cde7a16SDavid Greenman 	size = round_page(size);
1172f7af3dbSAlan Cox 	pindex = OFF_TO_IDX(foff + size);
1186cde7a16SDavid Greenman 
119df8bae1dSRodney W. Grimes 	/*
120c8664f82SAlan Cox 	 * Make sure this device can be mapped.
121c8664f82SAlan Cox 	 */
122c8664f82SAlan Cox 	dev = handle;
123751fdd08SPoul-Henning Kamp 	csw = dev_refthread(dev);
124751fdd08SPoul-Henning Kamp 	if (csw == NULL)
125c8664f82SAlan Cox 		return (NULL);
126c8664f82SAlan Cox 
127c8664f82SAlan Cox 	/*
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)
135751fdd08SPoul-Henning Kamp 		if ((*csw->d_mmap)(dev, off, &paddr, (int)prot) != 0) {
136751fdd08SPoul-Henning Kamp 			dev_relthread(dev);
137df8bae1dSRodney W. Grimes 			return (NULL);
138c8664f82SAlan Cox 		}
139df8bae1dSRodney W. Grimes 
140df8bae1dSRodney W. Grimes 	/*
141956f3135SPhilippe Charnier 	 * Lock to prevent object creation race condition.
14224a1cce3SDavid Greenman 	 */
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 		/*
152df8bae1dSRodney W. Grimes 		 * Allocate object and associate it with the pager.
153df8bae1dSRodney W. Grimes 		 */
154deea654eSKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
155deea654eSKonstantin Belousov 		object1 = vm_object_allocate(OBJT_DEVICE, pindex);
156deea654eSKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
157deea654eSKonstantin Belousov 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
158deea654eSKonstantin Belousov 		if (object != NULL) {
159deea654eSKonstantin Belousov 			/*
160deea654eSKonstantin Belousov 			 * We raced with other thread while allocating object.
161deea654eSKonstantin Belousov 			 */
162deea654eSKonstantin Belousov 			if (pindex > object->size)
163deea654eSKonstantin Belousov 				object->size = pindex;
164deea654eSKonstantin Belousov 		} else {
165deea654eSKonstantin Belousov 			object = object1;
166deea654eSKonstantin Belousov 			object1 = NULL;
16724a1cce3SDavid Greenman 			object->handle = handle;
16824a1cce3SDavid Greenman 			TAILQ_INIT(&object->un_pager.devp.devp_pglist);
169deea654eSKonstantin Belousov 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
170deea654eSKonstantin Belousov 			    pager_object_list);
171deea654eSKonstantin Belousov 		}
172df8bae1dSRodney W. Grimes 	} else {
1732f7af3dbSAlan Cox 		if (pindex > object->size)
1742f7af3dbSAlan Cox 			object->size = pindex;
175df8bae1dSRodney W. Grimes 	}
176deea654eSKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
177751fdd08SPoul-Henning Kamp 	dev_relthread(dev);
178deea654eSKonstantin Belousov 	vm_object_deallocate(object1);
17924a1cce3SDavid Greenman 	return (object);
18024a1cce3SDavid Greenman }
18124a1cce3SDavid Greenman 
182f708ef1bSPoul-Henning Kamp static void
18324a1cce3SDavid Greenman dev_pager_dealloc(object)
184df8bae1dSRodney W. Grimes 	vm_object_t object;
18524a1cce3SDavid Greenman {
186df8bae1dSRodney W. Grimes 	vm_page_t m;
187df8bae1dSRodney W. Grimes 
188deea654eSKonstantin Belousov 	VM_OBJECT_UNLOCK(object);
189a9fa2c05SAlfred Perlstein 	mtx_lock(&dev_pager_mtx);
19024a1cce3SDavid Greenman 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
191a9fa2c05SAlfred Perlstein 	mtx_unlock(&dev_pager_mtx);
192deea654eSKonstantin Belousov 	VM_OBJECT_LOCK(object);
193df8bae1dSRodney W. Grimes 	/*
194df8bae1dSRodney W. Grimes 	 * Free up our fake pages.
195df8bae1dSRodney W. Grimes 	 */
196b18bfc3dSJohn Dyson 	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
19724a1cce3SDavid Greenman 		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
198df8bae1dSRodney W. Grimes 		dev_pager_putfake(m);
199df8bae1dSRodney W. Grimes 	}
200df8bae1dSRodney W. Grimes }
201df8bae1dSRodney W. Grimes 
202f708ef1bSPoul-Henning Kamp static int
20324a1cce3SDavid Greenman dev_pager_getpages(object, m, count, reqpage)
20424a1cce3SDavid Greenman 	vm_object_t object;
20524a1cce3SDavid Greenman 	vm_page_t *m;
20624a1cce3SDavid Greenman 	int count;
20724a1cce3SDavid Greenman 	int reqpage;
208df8bae1dSRodney W. Grimes {
2096395da54SIan Dowse 	vm_pindex_t offset;
210227f9a1cSJake Burkholder 	vm_paddr_t paddr;
211df8bae1dSRodney W. Grimes 	vm_page_t page;
21289c9c53dSPoul-Henning Kamp 	struct cdev *dev;
21307159f9cSMaxime Henrion 	int i, ret;
214cac597e4SBruce Evans 	int prot;
215751fdd08SPoul-Henning Kamp 	struct cdevsw *csw;
216df8bae1dSRodney W. Grimes 
217a8ab4870SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
218ebb4a317SBruce Evans 	dev = object->handle;
2191c7c3c6aSMatthew Dillon 	offset = m[reqpage]->pindex;
2208630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
221751fdd08SPoul-Henning Kamp 	csw = dev_refthread(dev);
222751fdd08SPoul-Henning Kamp 	if (csw == NULL)
223751fdd08SPoul-Henning Kamp 		panic("dev_pager_getpage: no cdevsw");
224df8bae1dSRodney W. Grimes 	prot = PROT_READ;	/* XXX should pass in? */
22526f9a767SRodney W. Grimes 
226751fdd08SPoul-Henning Kamp 	ret = (*csw->d_mmap)(dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr, prot);
22707159f9cSMaxime Henrion 	KASSERT(ret == 0, ("dev_pager_getpage: map function returns error"));
228751fdd08SPoul-Henning Kamp 	dev_relthread(dev);
22987aefa49SAlan Cox 
23092bab635SDoug Rabson 	if ((m[reqpage]->flags & PG_FICTITIOUS) != 0) {
23192bab635SDoug Rabson 		/*
23292bab635SDoug Rabson 		 * If the passed in reqpage page is a fake page, update it with
23392bab635SDoug Rabson 		 * the new physical address.
23492bab635SDoug Rabson 		 */
23592bab635SDoug Rabson 		VM_OBJECT_LOCK(object);
236c413d99cSDoug Rabson 		dev_pager_updatefake(m[reqpage], paddr);
23792bab635SDoug Rabson 		if (count > 1) {
23892bab635SDoug Rabson 			vm_page_lock_queues();
23992bab635SDoug Rabson 			for (i = 0; i < count; i++) {
24092bab635SDoug Rabson 				if (i != reqpage)
24192bab635SDoug Rabson 					vm_page_free(m[i]);
24292bab635SDoug Rabson 			}
24392bab635SDoug Rabson 			vm_page_unlock_queues();
24492bab635SDoug Rabson 		}
24592bab635SDoug Rabson 	} else {
246df8bae1dSRodney W. Grimes 		/*
24707159f9cSMaxime Henrion 		 * Replace the passed in reqpage page with our own fake page and
24807159f9cSMaxime Henrion 		 * free up the all of the original pages.
249df8bae1dSRodney W. Grimes 		 */
250df8bae1dSRodney W. Grimes 		page = dev_pager_getfake(paddr);
2518630c117SAlan Cox 		VM_OBJECT_LOCK(object);
25224a1cce3SDavid Greenman 		TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
25360e15726SAlan Cox 		vm_page_lock_queues();
25460e15726SAlan Cox 		for (i = 0; i < count; i++)
25524a1cce3SDavid Greenman 			vm_page_free(m[i]);
25660e15726SAlan Cox 		vm_page_unlock_queues();
25726f9a767SRodney W. Grimes 		vm_page_insert(page, object, offset);
2589aa3d17dSAlan Cox 		m[reqpage] = page;
25992bab635SDoug Rabson 	}
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 	return (VM_PAGER_OK);
262df8bae1dSRodney W. Grimes }
263df8bae1dSRodney W. Grimes 
264e4542174SMatthew Dillon static void
26524a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals)
26624a1cce3SDavid Greenman 	vm_object_t object;
26724a1cce3SDavid Greenman 	vm_page_t *m;
26824a1cce3SDavid Greenman 	int count;
269df8bae1dSRodney W. Grimes 	boolean_t sync;
27024a1cce3SDavid Greenman 	int *rtvals;
271df8bae1dSRodney W. Grimes {
272df8bae1dSRodney W. Grimes 	panic("dev_pager_putpage called");
273df8bae1dSRodney W. Grimes }
274df8bae1dSRodney W. Grimes 
275f708ef1bSPoul-Henning Kamp static boolean_t
276a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after)
27724a1cce3SDavid Greenman 	vm_object_t object;
278a316d390SJohn Dyson 	vm_pindex_t pindex;
27924a1cce3SDavid Greenman 	int *before;
28024a1cce3SDavid Greenman 	int *after;
281df8bae1dSRodney W. Grimes {
28224a1cce3SDavid Greenman 	if (before != NULL)
28324a1cce3SDavid Greenman 		*before = 0;
28424a1cce3SDavid Greenman 	if (after != NULL)
28524a1cce3SDavid Greenman 		*after = 0;
286df8bae1dSRodney W. Grimes 	return (TRUE);
287df8bae1dSRodney W. Grimes }
288df8bae1dSRodney W. Grimes 
28925f2e1c8SAlan Cox /*
29025f2e1c8SAlan Cox  * Instantiate a fictitious page.  Unlike physical memory pages, only
29125f2e1c8SAlan Cox  * the machine-independent fields must be initialized.
29225f2e1c8SAlan Cox  */
293df8bae1dSRodney W. Grimes static vm_page_t
294df8bae1dSRodney W. Grimes dev_pager_getfake(paddr)
295227f9a1cSJake Burkholder 	vm_paddr_t paddr;
296df8bae1dSRodney W. Grimes {
297df8bae1dSRodney W. Grimes 	vm_page_t m;
298df8bae1dSRodney W. Grimes 
299a163d034SWarner Losh 	m = uma_zalloc(fakepg_zone, M_WAITOK);
30026f9a767SRodney W. Grimes 
3019af80719SAlan Cox 	m->flags = PG_FICTITIOUS;
3029af80719SAlan Cox 	m->oflags = VPO_BUSY;
3030d94caffSDavid Greenman 	m->valid = VM_PAGE_BITS_ALL;
30424a1cce3SDavid Greenman 	m->dirty = 0;
3050d94caffSDavid Greenman 	m->busy = 0;
306bd7e5f99SJohn Dyson 	m->queue = PQ_NONE;
307bb7db2c0SDavid Greenman 	m->object = NULL;
30826f9a767SRodney W. Grimes 
309df8bae1dSRodney W. Grimes 	m->wire_count = 1;
310c68f9c92SJohn Dyson 	m->hold_count = 0;
31126f9a767SRodney W. Grimes 	m->phys_addr = paddr;
31226f9a767SRodney W. Grimes 
313df8bae1dSRodney W. Grimes 	return (m);
314df8bae1dSRodney W. Grimes }
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes static void
317df8bae1dSRodney W. Grimes dev_pager_putfake(m)
318df8bae1dSRodney W. Grimes 	vm_page_t m;
319df8bae1dSRodney W. Grimes {
320df8bae1dSRodney W. Grimes 	if (!(m->flags & PG_FICTITIOUS))
321df8bae1dSRodney W. Grimes 		panic("dev_pager_putfake: bad page");
322670d17b5SJeff Roberson 	uma_zfree(fakepg_zone, m);
323df8bae1dSRodney W. Grimes }
32492bab635SDoug Rabson 
32592bab635SDoug Rabson static void
32692bab635SDoug Rabson dev_pager_updatefake(m, paddr)
32792bab635SDoug Rabson 	vm_page_t m;
32892bab635SDoug Rabson 	vm_paddr_t paddr;
32992bab635SDoug Rabson {
33092bab635SDoug Rabson 	if (!(m->flags & PG_FICTITIOUS))
33192bab635SDoug Rabson 		panic("dev_pager_updatefake: bad page");
33292bab635SDoug Rabson 	m->phys_addr = paddr;
333c413d99cSDoug Rabson 	m->valid = VM_PAGE_BITS_ALL;
33492bab635SDoug Rabson }
335