xref: /freebsd/sys/vm/device_pager.c (revision e46cd4132c1882a63c179152684037306543d03e)
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 
140deea654eSKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
14124a1cce3SDavid Greenman 
14224a1cce3SDavid Greenman 	/*
143df8bae1dSRodney W. Grimes 	 * Look up pager, creating as necessary.
144df8bae1dSRodney W. Grimes 	 */
145deea654eSKonstantin Belousov 	object1 = NULL;
14624a1cce3SDavid Greenman 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
14724a1cce3SDavid Greenman 	if (object == NULL) {
148df8bae1dSRodney W. Grimes 		/*
149e46cd413SAlan Cox 		 * Allocate object and associate it with the pager.  Initialize
150e46cd413SAlan Cox 		 * the object's pg_color based upon the physical address of the
151e46cd413SAlan Cox 		 * device's memory.
152df8bae1dSRodney W. Grimes 		 */
153deea654eSKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
154deea654eSKonstantin Belousov 		object1 = vm_object_allocate(OBJT_DEVICE, pindex);
155e46cd413SAlan Cox 		object1->flags |= OBJ_COLORED;
156e46cd413SAlan Cox 		object1->pg_color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
157deea654eSKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
158deea654eSKonstantin Belousov 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
159deea654eSKonstantin Belousov 		if (object != NULL) {
160deea654eSKonstantin Belousov 			/*
161deea654eSKonstantin Belousov 			 * We raced with other thread while allocating object.
162deea654eSKonstantin Belousov 			 */
163deea654eSKonstantin Belousov 			if (pindex > object->size)
164deea654eSKonstantin Belousov 				object->size = pindex;
165deea654eSKonstantin Belousov 		} else {
166deea654eSKonstantin Belousov 			object = object1;
167deea654eSKonstantin Belousov 			object1 = NULL;
16824a1cce3SDavid Greenman 			object->handle = handle;
16924a1cce3SDavid Greenman 			TAILQ_INIT(&object->un_pager.devp.devp_pglist);
170deea654eSKonstantin Belousov 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
171deea654eSKonstantin Belousov 			    pager_object_list);
172deea654eSKonstantin Belousov 		}
173df8bae1dSRodney W. Grimes 	} else {
1742f7af3dbSAlan Cox 		if (pindex > object->size)
1752f7af3dbSAlan Cox 			object->size = pindex;
176df8bae1dSRodney W. Grimes 	}
177deea654eSKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
178751fdd08SPoul-Henning Kamp 	dev_relthread(dev);
179deea654eSKonstantin Belousov 	vm_object_deallocate(object1);
18024a1cce3SDavid Greenman 	return (object);
18124a1cce3SDavid Greenman }
18224a1cce3SDavid Greenman 
183f708ef1bSPoul-Henning Kamp static void
18424a1cce3SDavid Greenman dev_pager_dealloc(object)
185df8bae1dSRodney W. Grimes 	vm_object_t object;
18624a1cce3SDavid Greenman {
187df8bae1dSRodney W. Grimes 	vm_page_t m;
188df8bae1dSRodney W. Grimes 
189deea654eSKonstantin Belousov 	VM_OBJECT_UNLOCK(object);
190a9fa2c05SAlfred Perlstein 	mtx_lock(&dev_pager_mtx);
19124a1cce3SDavid Greenman 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
192a9fa2c05SAlfred Perlstein 	mtx_unlock(&dev_pager_mtx);
193deea654eSKonstantin Belousov 	VM_OBJECT_LOCK(object);
194df8bae1dSRodney W. Grimes 	/*
195df8bae1dSRodney W. Grimes 	 * Free up our fake pages.
196df8bae1dSRodney W. Grimes 	 */
197b18bfc3dSJohn Dyson 	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
19824a1cce3SDavid Greenman 		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
199df8bae1dSRodney W. Grimes 		dev_pager_putfake(m);
200df8bae1dSRodney W. Grimes 	}
201df8bae1dSRodney W. Grimes }
202df8bae1dSRodney W. Grimes 
203f708ef1bSPoul-Henning Kamp static int
20424a1cce3SDavid Greenman dev_pager_getpages(object, m, count, reqpage)
20524a1cce3SDavid Greenman 	vm_object_t object;
20624a1cce3SDavid Greenman 	vm_page_t *m;
20724a1cce3SDavid Greenman 	int count;
20824a1cce3SDavid Greenman 	int reqpage;
209df8bae1dSRodney W. Grimes {
2106395da54SIan Dowse 	vm_pindex_t offset;
211227f9a1cSJake Burkholder 	vm_paddr_t paddr;
212df8bae1dSRodney W. Grimes 	vm_page_t page;
21389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
21407159f9cSMaxime Henrion 	int i, ret;
215cac597e4SBruce Evans 	int prot;
216751fdd08SPoul-Henning Kamp 	struct cdevsw *csw;
217df8bae1dSRodney W. Grimes 
218a8ab4870SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
219ebb4a317SBruce Evans 	dev = object->handle;
2201c7c3c6aSMatthew Dillon 	offset = m[reqpage]->pindex;
2218630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
222751fdd08SPoul-Henning Kamp 	csw = dev_refthread(dev);
223751fdd08SPoul-Henning Kamp 	if (csw == NULL)
224751fdd08SPoul-Henning Kamp 		panic("dev_pager_getpage: no cdevsw");
225df8bae1dSRodney W. Grimes 	prot = PROT_READ;	/* XXX should pass in? */
22626f9a767SRodney W. Grimes 
227751fdd08SPoul-Henning Kamp 	ret = (*csw->d_mmap)(dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr, prot);
22807159f9cSMaxime Henrion 	KASSERT(ret == 0, ("dev_pager_getpage: map function returns error"));
229751fdd08SPoul-Henning Kamp 	dev_relthread(dev);
23087aefa49SAlan Cox 
23192bab635SDoug Rabson 	if ((m[reqpage]->flags & PG_FICTITIOUS) != 0) {
23292bab635SDoug Rabson 		/*
23392bab635SDoug Rabson 		 * If the passed in reqpage page is a fake page, update it with
23492bab635SDoug Rabson 		 * the new physical address.
23592bab635SDoug Rabson 		 */
23692bab635SDoug Rabson 		VM_OBJECT_LOCK(object);
237c413d99cSDoug Rabson 		dev_pager_updatefake(m[reqpage], paddr);
23892bab635SDoug Rabson 		if (count > 1) {
23992bab635SDoug Rabson 			vm_page_lock_queues();
24092bab635SDoug Rabson 			for (i = 0; i < count; i++) {
24192bab635SDoug Rabson 				if (i != reqpage)
24292bab635SDoug Rabson 					vm_page_free(m[i]);
24392bab635SDoug Rabson 			}
24492bab635SDoug Rabson 			vm_page_unlock_queues();
24592bab635SDoug Rabson 		}
24692bab635SDoug Rabson 	} else {
247df8bae1dSRodney W. Grimes 		/*
24807159f9cSMaxime Henrion 		 * Replace the passed in reqpage page with our own fake page and
24907159f9cSMaxime Henrion 		 * free up the all of the original pages.
250df8bae1dSRodney W. Grimes 		 */
251df8bae1dSRodney W. Grimes 		page = dev_pager_getfake(paddr);
2528630c117SAlan Cox 		VM_OBJECT_LOCK(object);
25324a1cce3SDavid Greenman 		TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
25460e15726SAlan Cox 		vm_page_lock_queues();
25560e15726SAlan Cox 		for (i = 0; i < count; i++)
25624a1cce3SDavid Greenman 			vm_page_free(m[i]);
25760e15726SAlan Cox 		vm_page_unlock_queues();
25826f9a767SRodney W. Grimes 		vm_page_insert(page, object, offset);
2599aa3d17dSAlan Cox 		m[reqpage] = page;
26092bab635SDoug Rabson 	}
261df8bae1dSRodney W. Grimes 
262df8bae1dSRodney W. Grimes 	return (VM_PAGER_OK);
263df8bae1dSRodney W. Grimes }
264df8bae1dSRodney W. Grimes 
265e4542174SMatthew Dillon static void
26624a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals)
26724a1cce3SDavid Greenman 	vm_object_t object;
26824a1cce3SDavid Greenman 	vm_page_t *m;
26924a1cce3SDavid Greenman 	int count;
270df8bae1dSRodney W. Grimes 	boolean_t sync;
27124a1cce3SDavid Greenman 	int *rtvals;
272df8bae1dSRodney W. Grimes {
273df8bae1dSRodney W. Grimes 	panic("dev_pager_putpage called");
274df8bae1dSRodney W. Grimes }
275df8bae1dSRodney W. Grimes 
276f708ef1bSPoul-Henning Kamp static boolean_t
277a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after)
27824a1cce3SDavid Greenman 	vm_object_t object;
279a316d390SJohn Dyson 	vm_pindex_t pindex;
28024a1cce3SDavid Greenman 	int *before;
28124a1cce3SDavid Greenman 	int *after;
282df8bae1dSRodney W. Grimes {
28324a1cce3SDavid Greenman 	if (before != NULL)
28424a1cce3SDavid Greenman 		*before = 0;
28524a1cce3SDavid Greenman 	if (after != NULL)
28624a1cce3SDavid Greenman 		*after = 0;
287df8bae1dSRodney W. Grimes 	return (TRUE);
288df8bae1dSRodney W. Grimes }
289df8bae1dSRodney W. Grimes 
29025f2e1c8SAlan Cox /*
29125f2e1c8SAlan Cox  * Instantiate a fictitious page.  Unlike physical memory pages, only
29225f2e1c8SAlan Cox  * the machine-independent fields must be initialized.
29325f2e1c8SAlan Cox  */
294df8bae1dSRodney W. Grimes static vm_page_t
295df8bae1dSRodney W. Grimes dev_pager_getfake(paddr)
296227f9a1cSJake Burkholder 	vm_paddr_t paddr;
297df8bae1dSRodney W. Grimes {
298df8bae1dSRodney W. Grimes 	vm_page_t m;
299df8bae1dSRodney W. Grimes 
300a163d034SWarner Losh 	m = uma_zalloc(fakepg_zone, M_WAITOK);
30126f9a767SRodney W. Grimes 
3029af80719SAlan Cox 	m->flags = PG_FICTITIOUS;
3039af80719SAlan Cox 	m->oflags = VPO_BUSY;
3040d94caffSDavid Greenman 	m->valid = VM_PAGE_BITS_ALL;
30524a1cce3SDavid Greenman 	m->dirty = 0;
3060d94caffSDavid Greenman 	m->busy = 0;
307bd7e5f99SJohn Dyson 	m->queue = PQ_NONE;
308bb7db2c0SDavid Greenman 	m->object = NULL;
30926f9a767SRodney W. Grimes 
310df8bae1dSRodney W. Grimes 	m->wire_count = 1;
311c68f9c92SJohn Dyson 	m->hold_count = 0;
31226f9a767SRodney W. Grimes 	m->phys_addr = paddr;
31326f9a767SRodney W. Grimes 
314df8bae1dSRodney W. Grimes 	return (m);
315df8bae1dSRodney W. Grimes }
316df8bae1dSRodney W. Grimes 
317df8bae1dSRodney W. Grimes static void
318df8bae1dSRodney W. Grimes dev_pager_putfake(m)
319df8bae1dSRodney W. Grimes 	vm_page_t m;
320df8bae1dSRodney W. Grimes {
321df8bae1dSRodney W. Grimes 	if (!(m->flags & PG_FICTITIOUS))
322df8bae1dSRodney W. Grimes 		panic("dev_pager_putfake: bad page");
323670d17b5SJeff Roberson 	uma_zfree(fakepg_zone, m);
324df8bae1dSRodney W. Grimes }
32592bab635SDoug Rabson 
32692bab635SDoug Rabson static void
32792bab635SDoug Rabson dev_pager_updatefake(m, paddr)
32892bab635SDoug Rabson 	vm_page_t m;
32992bab635SDoug Rabson 	vm_paddr_t paddr;
33092bab635SDoug Rabson {
33192bab635SDoug Rabson 	if (!(m->flags & PG_FICTITIOUS))
33292bab635SDoug Rabson 		panic("dev_pager_updatefake: bad page");
33392bab635SDoug Rabson 	m->phys_addr = paddr;
334c413d99cSDoug Rabson 	m->valid = VM_PAGE_BITS_ALL;
33592bab635SDoug Rabson }
336