xref: /freebsd/sys/vm/device_pager.c (revision 10cf25607484ea85dc2fe22b20e08b824f91a45a)
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,
573364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *);
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 
70df8bae1dSRodney W. Grimes struct pagerops devicepagerops = {
714e658600SPoul-Henning Kamp 	.pgo_init =	dev_pager_init,
724e658600SPoul-Henning Kamp 	.pgo_alloc =	dev_pager_alloc,
734e658600SPoul-Henning Kamp 	.pgo_dealloc =	dev_pager_dealloc,
744e658600SPoul-Henning Kamp 	.pgo_getpages =	dev_pager_getpages,
754e658600SPoul-Henning Kamp 	.pgo_putpages =	dev_pager_putpages,
764e658600SPoul-Henning Kamp 	.pgo_haspage =	dev_pager_haspage,
77df8bae1dSRodney W. Grimes };
78df8bae1dSRodney W. Grimes 
79f708ef1bSPoul-Henning Kamp static void
80df8bae1dSRodney W. Grimes dev_pager_init()
81df8bae1dSRodney W. Grimes {
8224a1cce3SDavid Greenman 	TAILQ_INIT(&dev_pager_object_list);
836008862bSJohn Baldwin 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
84df8bae1dSRodney W. Grimes }
85df8bae1dSRodney W. Grimes 
86c8664f82SAlan Cox /*
87c8664f82SAlan Cox  * MPSAFE
88c8664f82SAlan Cox  */
89f708ef1bSPoul-Henning Kamp static vm_object_t
903364c323SKonstantin Belousov dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
913364c323SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred)
92df8bae1dSRodney W. Grimes {
9389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
94deea654eSKonstantin Belousov 	vm_object_t object, object1;
952f7af3dbSAlan Cox 	vm_pindex_t pindex;
967095ee91SDoug Rabson 	unsigned int npages;
97227f9a1cSJake Burkholder 	vm_paddr_t paddr;
98cfd7baceSRobert Noland 	vm_ooffset_t off;
992fa8c8d2SJohn Baldwin 	vm_memattr_t dummy;
100751fdd08SPoul-Henning Kamp 	struct cdevsw *csw;
1013979450bSKonstantin Belousov 	int ref;
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes 	/*
104df8bae1dSRodney W. Grimes 	 * Offset should be page aligned.
105df8bae1dSRodney W. Grimes 	 */
106aa8de40aSPoul-Henning Kamp 	if (foff & PAGE_MASK)
107df8bae1dSRodney W. Grimes 		return (NULL);
108df8bae1dSRodney W. Grimes 
1096cde7a16SDavid Greenman 	size = round_page(size);
1102f7af3dbSAlan Cox 	pindex = OFF_TO_IDX(foff + size);
1116cde7a16SDavid Greenman 
112df8bae1dSRodney W. Grimes 	/*
113c8664f82SAlan Cox 	 * Make sure this device can be mapped.
114c8664f82SAlan Cox 	 */
115c8664f82SAlan Cox 	dev = handle;
1163979450bSKonstantin Belousov 	csw = dev_refthread(dev, &ref);
117751fdd08SPoul-Henning Kamp 	if (csw == NULL)
118c8664f82SAlan Cox 		return (NULL);
119c8664f82SAlan Cox 
120c8664f82SAlan Cox 	/*
1210d94caffSDavid Greenman 	 * Check that the specified range of the device allows the desired
1220d94caffSDavid Greenman 	 * protection.
123df8bae1dSRodney W. Grimes 	 *
124df8bae1dSRodney W. Grimes 	 * XXX assumes VM_PROT_* == PROT_*
125df8bae1dSRodney W. Grimes 	 */
1266cde7a16SDavid Greenman 	npages = OFF_TO_IDX(size);
127df8bae1dSRodney W. Grimes 	for (off = foff; npages--; off += PAGE_SIZE)
128cfd7baceSRobert Noland 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
1293979450bSKonstantin Belousov 			dev_relthread(dev, ref);
130df8bae1dSRodney W. Grimes 			return (NULL);
131c8664f82SAlan Cox 		}
132df8bae1dSRodney W. Grimes 
133deea654eSKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
13424a1cce3SDavid Greenman 
13524a1cce3SDavid Greenman 	/*
136df8bae1dSRodney W. Grimes 	 * Look up pager, creating as necessary.
137df8bae1dSRodney W. Grimes 	 */
138deea654eSKonstantin Belousov 	object1 = NULL;
13924a1cce3SDavid Greenman 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
14024a1cce3SDavid Greenman 	if (object == NULL) {
141df8bae1dSRodney W. Grimes 		/*
142e46cd413SAlan Cox 		 * Allocate object and associate it with the pager.  Initialize
143e46cd413SAlan Cox 		 * the object's pg_color based upon the physical address of the
144e46cd413SAlan Cox 		 * device's memory.
145df8bae1dSRodney W. Grimes 		 */
146deea654eSKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
147deea654eSKonstantin Belousov 		object1 = vm_object_allocate(OBJT_DEVICE, pindex);
148e46cd413SAlan Cox 		object1->flags |= OBJ_COLORED;
149e46cd413SAlan Cox 		object1->pg_color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
150deea654eSKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
151deea654eSKonstantin Belousov 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
152deea654eSKonstantin Belousov 		if (object != NULL) {
153deea654eSKonstantin Belousov 			/*
154deea654eSKonstantin Belousov 			 * We raced with other thread while allocating object.
155deea654eSKonstantin Belousov 			 */
156deea654eSKonstantin Belousov 			if (pindex > object->size)
157deea654eSKonstantin Belousov 				object->size = pindex;
158deea654eSKonstantin Belousov 		} else {
159deea654eSKonstantin Belousov 			object = object1;
160deea654eSKonstantin Belousov 			object1 = NULL;
16124a1cce3SDavid Greenman 			object->handle = handle;
16224a1cce3SDavid Greenman 			TAILQ_INIT(&object->un_pager.devp.devp_pglist);
163deea654eSKonstantin Belousov 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
164deea654eSKonstantin Belousov 			    pager_object_list);
165deea654eSKonstantin Belousov 		}
166df8bae1dSRodney W. Grimes 	} else {
1672f7af3dbSAlan Cox 		if (pindex > object->size)
1682f7af3dbSAlan Cox 			object->size = pindex;
169df8bae1dSRodney W. Grimes 	}
170deea654eSKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
1713979450bSKonstantin Belousov 	dev_relthread(dev, ref);
172deea654eSKonstantin Belousov 	vm_object_deallocate(object1);
17324a1cce3SDavid Greenman 	return (object);
17424a1cce3SDavid Greenman }
17524a1cce3SDavid Greenman 
176f708ef1bSPoul-Henning Kamp static void
17724a1cce3SDavid Greenman dev_pager_dealloc(object)
178df8bae1dSRodney W. Grimes 	vm_object_t object;
17924a1cce3SDavid Greenman {
180df8bae1dSRodney W. Grimes 	vm_page_t m;
181df8bae1dSRodney W. Grimes 
182deea654eSKonstantin Belousov 	VM_OBJECT_UNLOCK(object);
183a9fa2c05SAlfred Perlstein 	mtx_lock(&dev_pager_mtx);
18424a1cce3SDavid Greenman 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
185a9fa2c05SAlfred Perlstein 	mtx_unlock(&dev_pager_mtx);
186deea654eSKonstantin Belousov 	VM_OBJECT_LOCK(object);
187df8bae1dSRodney W. Grimes 	/*
188df8bae1dSRodney W. Grimes 	 * Free up our fake pages.
189df8bae1dSRodney W. Grimes 	 */
190ce1b857fSAlan Cox 	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != NULL) {
19124a1cce3SDavid Greenman 		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
192*10cf2560SAlan Cox 		vm_page_putfake(m);
193df8bae1dSRodney W. Grimes 	}
194df8bae1dSRodney W. Grimes }
195df8bae1dSRodney W. Grimes 
196f708ef1bSPoul-Henning Kamp static int
19724a1cce3SDavid Greenman dev_pager_getpages(object, m, count, reqpage)
19824a1cce3SDavid Greenman 	vm_object_t object;
19924a1cce3SDavid Greenman 	vm_page_t *m;
20024a1cce3SDavid Greenman 	int count;
20124a1cce3SDavid Greenman 	int reqpage;
202df8bae1dSRodney W. Grimes {
2036395da54SIan Dowse 	vm_pindex_t offset;
204227f9a1cSJake Burkholder 	vm_paddr_t paddr;
2053153e878SAlan Cox 	vm_page_t m_paddr, page;
2063153e878SAlan Cox 	vm_memattr_t memattr;
20789c9c53dSPoul-Henning Kamp 	struct cdev *dev;
2083979450bSKonstantin Belousov 	int i, ref, ret;
209751fdd08SPoul-Henning Kamp 	struct cdevsw *csw;
2107818e0a5SKonstantin Belousov 	struct thread *td;
2117818e0a5SKonstantin Belousov 	struct file *fpop;
212df8bae1dSRodney W. Grimes 
213a8ab4870SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
214ebb4a317SBruce Evans 	dev = object->handle;
215ce1b857fSAlan Cox 	page = m[reqpage];
216ce1b857fSAlan Cox 	offset = page->pindex;
2173153e878SAlan Cox 	memattr = object->memattr;
2188630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
2193979450bSKonstantin Belousov 	csw = dev_refthread(dev, &ref);
220751fdd08SPoul-Henning Kamp 	if (csw == NULL)
221751fdd08SPoul-Henning Kamp 		panic("dev_pager_getpage: no cdevsw");
2227818e0a5SKonstantin Belousov 	td = curthread;
2237818e0a5SKonstantin Belousov 	fpop = td->td_fpop;
2247818e0a5SKonstantin Belousov 	td->td_fpop = NULL;
225cfd7baceSRobert Noland 	ret = csw->d_mmap(dev, (vm_ooffset_t)offset << PAGE_SHIFT, &paddr,
2262fa8c8d2SJohn Baldwin 	    PROT_READ, &memattr);
22707159f9cSMaxime Henrion 	KASSERT(ret == 0, ("dev_pager_getpage: map function returns error"));
2287818e0a5SKonstantin Belousov 	td->td_fpop = fpop;
2293979450bSKonstantin Belousov 	dev_relthread(dev, ref);
2303153e878SAlan Cox 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
2313153e878SAlan Cox 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
2323153e878SAlan Cox 	    pmap_page_get_memattr(m_paddr) != memattr) {
2333153e878SAlan Cox 		memattr = pmap_page_get_memattr(m_paddr);
2343153e878SAlan Cox 		printf(
2353153e878SAlan Cox 	    "WARNING: A device driver has set \"memattr\" inconsistently.\n");
2363153e878SAlan Cox 	}
237ce1b857fSAlan Cox 	if ((page->flags & PG_FICTITIOUS) != 0) {
23892bab635SDoug Rabson 		/*
23992bab635SDoug Rabson 		 * If the passed in reqpage page is a fake page, update it with
24092bab635SDoug Rabson 		 * the new physical address.
24192bab635SDoug Rabson 		 */
24292bab635SDoug Rabson 		VM_OBJECT_LOCK(object);
243*10cf2560SAlan Cox 		vm_page_updatefake(page, paddr, memattr);
24492bab635SDoug Rabson 		if (count > 1) {
2452965a453SKip Macy 
24692bab635SDoug Rabson 			for (i = 0; i < count; i++) {
2472965a453SKip Macy 				if (i != reqpage) {
2482965a453SKip Macy 					vm_page_lock(m[i]);
24992bab635SDoug Rabson 					vm_page_free(m[i]);
2502965a453SKip Macy 					vm_page_unlock(m[i]);
2512965a453SKip Macy 				}
2522965a453SKip Macy 			}
25392bab635SDoug Rabson 		}
25492bab635SDoug Rabson 	} else {
255df8bae1dSRodney W. Grimes 		/*
25607159f9cSMaxime Henrion 		 * Replace the passed in reqpage page with our own fake page and
25707159f9cSMaxime Henrion 		 * free up the all of the original pages.
258df8bae1dSRodney W. Grimes 		 */
259*10cf2560SAlan Cox 		page = vm_page_getfake(paddr, memattr);
2608630c117SAlan Cox 		VM_OBJECT_LOCK(object);
26124a1cce3SDavid Greenman 		TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
2622965a453SKip Macy 		for (i = 0; i < count; i++) {
2632965a453SKip Macy 			vm_page_lock(m[i]);
26424a1cce3SDavid Greenman 			vm_page_free(m[i]);
2652965a453SKip Macy 			vm_page_unlock(m[i]);
2662965a453SKip Macy 		}
26726f9a767SRodney W. Grimes 		vm_page_insert(page, object, offset);
2689aa3d17dSAlan Cox 		m[reqpage] = page;
26992bab635SDoug Rabson 	}
270ce1b857fSAlan Cox 	page->valid = VM_PAGE_BITS_ALL;
271df8bae1dSRodney W. Grimes 	return (VM_PAGER_OK);
272df8bae1dSRodney W. Grimes }
273df8bae1dSRodney W. Grimes 
274e4542174SMatthew Dillon static void
27524a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals)
27624a1cce3SDavid Greenman 	vm_object_t object;
27724a1cce3SDavid Greenman 	vm_page_t *m;
27824a1cce3SDavid Greenman 	int count;
279df8bae1dSRodney W. Grimes 	boolean_t sync;
28024a1cce3SDavid Greenman 	int *rtvals;
281df8bae1dSRodney W. Grimes {
282df8bae1dSRodney W. Grimes 	panic("dev_pager_putpage called");
283df8bae1dSRodney W. Grimes }
284df8bae1dSRodney W. Grimes 
285f708ef1bSPoul-Henning Kamp static boolean_t
286a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after)
28724a1cce3SDavid Greenman 	vm_object_t object;
288a316d390SJohn Dyson 	vm_pindex_t pindex;
28924a1cce3SDavid Greenman 	int *before;
29024a1cce3SDavid Greenman 	int *after;
291df8bae1dSRodney W. Grimes {
29224a1cce3SDavid Greenman 	if (before != NULL)
29324a1cce3SDavid Greenman 		*before = 0;
29424a1cce3SDavid Greenman 	if (after != NULL)
29524a1cce3SDavid Greenman 		*after = 0;
296df8bae1dSRodney W. Grimes 	return (TRUE);
297df8bae1dSRodney W. Grimes }
298