device_pager.c revision 92727
150477Speter/*
21817Sdg * Copyright (c) 1990 University of Utah.
31817Sdg * Copyright (c) 1991, 1993
41541Srgrimes *	The Regents of the University of California.  All rights reserved.
51541Srgrimes *
61541Srgrimes * This code is derived from software contributed to Berkeley by
7160798Sjhb * the Systems Programming Group of the University of Utah Computer
81541Srgrimes * Science Department.
9146806Srwatson *
10146806Srwatson * Redistribution and use in source and binary forms, with or without
11146806Srwatson * modification, are permitted provided that the following conditions
12146806Srwatson * are met:
13146806Srwatson * 1. Redistributions of source code must retain the above copyright
14194390Sjhb *    notice, this list of conditions and the following disclaimer.
15203660Sed * 2. Redistributions in binary form must reproduce the above copyright
16194390Sjhb *    notice, this list of conditions and the following disclaimer in the
17194390Sjhb *    documentation and/or other materials provided with the distribution.
1811294Sswallace * 3. All advertising materials mentioning features or use of this software
1910905Sbde *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
2110905Sbde *	California, Berkeley and its contributors.
2210905Sbde * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2899855Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29194645Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30194833Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3369449Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34194383Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35160797Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36181972Sobrien * SUCH DAMAGE.
37181972Sobrien *
38183361Sjhb *	@(#)device_pager.c	8.1 (Berkeley) 6/11/93
39181972Sobrien * $FreeBSD: head/sys/vm/device_pager.c 92727 2002-03-19 22:20:14Z alfred $
40181972Sobrien */
41181972Sobrien
42181972Sobrien#include <sys/param.h>
43211838Skib#include <sys/systm.h>
44104747Srwatson#include <sys/conf.h>
45104747Srwatson#include <sys/lock.h>
46123408Speter#include <sys/proc.h>
47123408Speter#include <sys/mutex.h>
481541Srgrimes#include <sys/mman.h>
491541Srgrimes#include <sys/sx.h>
5011294Sswallace
5111294Sswallace#include <vm/vm.h>
5211294Sswallace#include <vm/vm_object.h>
5311294Sswallace#include <vm/vm_page.h>
541541Srgrimes#include <vm/vm_pager.h>
551541Srgrimes#include <vm/vm_zone.h>
561541Srgrimes
571541Srgrimesstatic void dev_pager_init(void);
581541Srgrimesstatic vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
591541Srgrimes		vm_ooffset_t);
60160798Sjhbstatic void dev_pager_dealloc(vm_object_t);
61160798Sjhbstatic int dev_pager_getpages(vm_object_t, vm_page_t *, int, int);
62146806Srwatsonstatic void dev_pager_putpages(vm_object_t, vm_page_t *, int,
63160798Sjhb		boolean_t, int *);
64160798Sjhbstatic boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *,
65146806Srwatson		int *);
66160798Sjhb
67146806Srwatson/* list of device pager objects */
68160798Sjhbstatic struct pagerlst dev_pager_object_list;
6912216Sbde/* protect against object creation */
7012216Sbdestatic struct sx dev_pager_sx;
7112216Sbde/* protect list manipulation */
72160798Sjhbstatic struct mtx dev_pager_mtx;
73160798Sjhb
74242958Skib
75162991Srwatsonstatic vm_zone_t fakepg_zone;
76160798Sjhb#if 0
77160798Sjhbstatic struct vm_zone fakepg_zone_store;
78146806Srwatson#endif
79160798Sjhb
80160798Sjhbstatic vm_page_t dev_pager_getfake(vm_offset_t);
81160798Sjhbstatic void dev_pager_putfake(vm_page_t);
82160798Sjhb
83160798Sjhbstruct pagerops devicepagerops = {
84160798Sjhb	dev_pager_init,
85146806Srwatson	dev_pager_alloc,
86160798Sjhb	dev_pager_dealloc,
87146806Srwatson	dev_pager_getpages,
88160798Sjhb	dev_pager_putpages,
89146806Srwatson	dev_pager_haspage,
90160798Sjhb	NULL
91160798Sjhb};
92146806Srwatson
9312216Sbdestatic void
94160798Sjhbdev_pager_init()
95160798Sjhb{
96160798Sjhb	TAILQ_INIT(&dev_pager_object_list);
97160798Sjhb	sx_init(&dev_pager_sx, "dev_pager create");
98160798Sjhb	mtx_init(&dev_pager_mtx, "dev_pager list", MTX_DEF);
99146806Srwatson#if 0
100160798Sjhb	fakepg_zone = &fakepg_zone_store;
101146806Srwatson	zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2);
102160798Sjhb#endif
103146806Srwatson	fakepg_zone = zinit("DP fakepg", sizeof(struct vm_page), 0, 0, 0);
104160798Sjhb}
105146806Srwatson
106146806Srwatsonstatic vm_object_t
107146806Srwatsondev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
108160798Sjhb{
109146806Srwatson	dev_t dev;
110146806Srwatson	d_mmap_t *mapfunc;
111160798Sjhb	vm_object_t object;
112146806Srwatson	unsigned int npages;
113146806Srwatson	vm_offset_t off;
114160798Sjhb
115146806Srwatson	mtx_assert(&Giant, MA_OWNED);
116146806Srwatson	/*
117227691Sed	 * Make sure this device can be mapped.
118248597Spjd	 */
119248597Spjd	dev = handle;
120160798Sjhb	mapfunc = devsw(dev)->d_mmap;
121160798Sjhb	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) {
122160798Sjhb		printf("obsolete map function %p\n", (void *)mapfunc);
123160798Sjhb		return (NULL);
124160798Sjhb	}
125160798Sjhb
126160798Sjhb	/*
127160798Sjhb	 * Offset should be page aligned.
128160798Sjhb	 */
129146806Srwatson	if (foff & PAGE_MASK)
130160798Sjhb		return (NULL);
131146806Srwatson
132160798Sjhb	size = round_page(size);
133146806Srwatson
134146806Srwatson	/*
135160798Sjhb	 * Check that the specified range of the device allows the desired
136160798Sjhb	 * protection.
13721776Sbde	 *
13821776Sbde	 * XXX assumes VM_PROT_* == PROT_*
13921776Sbde	 */
140160798Sjhb	npages = OFF_TO_IDX(size);
141146806Srwatson	for (off = foff; npages--; off += PAGE_SIZE)
142160798Sjhb		if ((*mapfunc) (dev, off, (int) prot) == -1)
143160798Sjhb			return (NULL);
144160798Sjhb
145162373Srwatson	/*
146146806Srwatson	 * Lock to prevent object creation race condition.
147160798Sjhb	 */
148146806Srwatson	sx_xlock(&dev_pager_sx);
149160798Sjhb
150160798Sjhb	/*
151160798Sjhb	 * Look up pager, creating as necessary.
152176215Sru	 */
153176215Sru	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
154160798Sjhb	if (object == NULL) {
155146806Srwatson		/*
156160798Sjhb		 * Allocate object and associate it with the pager.
157146806Srwatson		 */
158160798Sjhb		object = vm_object_allocate(OBJT_DEVICE,
159160798Sjhb			OFF_TO_IDX(foff + size));
160160798Sjhb		object->handle = handle;
161146806Srwatson		TAILQ_INIT(&object->un_pager.devp.devp_pglist);
162146806Srwatson		mtx_lock(&dev_pager_mtx);
163162991Srwatson		TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
164146806Srwatson		mtx_unlock(&dev_pager_mtx);
165160798Sjhb	} else {
166146806Srwatson		/*
167160798Sjhb		 * Gain a reference to the object.
168146806Srwatson		 */
169146806Srwatson		vm_object_reference(object);
170160798Sjhb		if (OFF_TO_IDX(foff + size) > object->size)
171160798Sjhb			object->size = OFF_TO_IDX(foff + size);
172160798Sjhb	}
173146806Srwatson
174160798Sjhb	sx_xunlock(&dev_pager_sx);
175146806Srwatson
176160798Sjhb	return (object);
177160798Sjhb}
178146806Srwatson
179160798Sjhbstatic void
180146806Srwatsondev_pager_dealloc(object)
181146806Srwatson	vm_object_t object;
182146806Srwatson{
183160798Sjhb	vm_page_t m;
184146806Srwatson
185160798Sjhb	mtx_lock(&dev_pager_mtx);
186146806Srwatson	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
187160798Sjhb	mtx_unlock(&dev_pager_mtx);
188146806Srwatson	/*
189160798Sjhb	 * Free up our fake pages.
190160798Sjhb	 */
191160798Sjhb	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
192146806Srwatson		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
193160798Sjhb		dev_pager_putfake(m);
194160798Sjhb	}
195160798Sjhb}
196146806Srwatson
197160798Sjhbstatic int
198146806Srwatsondev_pager_getpages(object, m, count, reqpage)
199146806Srwatson	vm_object_t object;
200160798Sjhb	vm_page_t *m;
201146806Srwatson	int count;
202146806Srwatson	int reqpage;
203160798Sjhb{
204160798Sjhb	vm_offset_t offset;
205146806Srwatson	vm_offset_t paddr;
206160798Sjhb	vm_page_t page;
207123750Speter	dev_t dev;
20812216Sbde	int i;
209160798Sjhb	d_mmap_t *mapfunc;
210146806Srwatson	int prot;
211146806Srwatson
212160798Sjhb	mtx_assert(&Giant, MA_OWNED);
213160798Sjhb	dev = object->handle;
214146806Srwatson	offset = m[reqpage]->pindex;
215160798Sjhb	prot = PROT_READ;	/* XXX should pass in? */
216146806Srwatson	mapfunc = devsw(dev)->d_mmap;
217160798Sjhb
218146806Srwatson	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop)
219194390Sjhb		panic("dev_pager_getpage: no map function");
220146806Srwatson
221160798Sjhb	paddr = pmap_phys_address((*mapfunc) (dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
222160798Sjhb	KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
223146806Srwatson	/*
224160798Sjhb	 * Replace the passed in reqpage page with our own fake page and free up the
225146806Srwatson	 * all of the original pages.
226160798Sjhb	 */
227146806Srwatson	page = dev_pager_getfake(paddr);
228160798Sjhb	TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
229146806Srwatson	for (i = 0; i < count; i++) {
230160798Sjhb		vm_page_free(m[i]);
231146806Srwatson	}
232160798Sjhb	vm_page_insert(page, object, offset);
233146806Srwatson
234160798Sjhb	return (VM_PAGER_OK);
235146806Srwatson}
236160798Sjhb
237160798Sjhbstatic void
238160798Sjhbdev_pager_putpages(object, m, count, sync, rtvals)
23921776Sbde	vm_object_t object;
24021776Sbde	vm_page_t *m;
241160798Sjhb	int count;
242146806Srwatson	boolean_t sync;
243160798Sjhb	int *rtvals;
244146806Srwatson{
245160798Sjhb	panic("dev_pager_putpage called");
246146806Srwatson}
247146806Srwatson
248160798Sjhbstatic boolean_t
249146806Srwatsondev_pager_haspage(object, pindex, before, after)
250160798Sjhb	vm_object_t object;
251146806Srwatson	vm_pindex_t pindex;
252160798Sjhb	int *before;
253146806Srwatson	int *after;
254146806Srwatson{
255160798Sjhb	if (before != NULL)
256146806Srwatson		*before = 0;
257160798Sjhb	if (after != NULL)
258146806Srwatson		*after = 0;
259160798Sjhb	return (TRUE);
260146806Srwatson}
261160798Sjhb
262160798Sjhbstatic vm_page_t
263194390Sjhbdev_pager_getfake(paddr)
264146806Srwatson	vm_offset_t paddr;
265146806Srwatson{
266146806Srwatson	vm_page_t m;
267160798Sjhb
268160798Sjhb	m = zalloc(fakepg_zone);
269160798Sjhb
270160798Sjhb	m->flags = PG_BUSY | PG_FICTITIOUS;
271160798Sjhb	m->valid = VM_PAGE_BITS_ALL;
272160798Sjhb	m->dirty = 0;
273160798Sjhb	m->busy = 0;
274160798Sjhb	m->queue = PQ_NONE;
275146806Srwatson	m->object = NULL;
276160798Sjhb
277160798Sjhb	m->wire_count = 1;
278146806Srwatson	m->hold_count = 0;
279160798Sjhb	m->phys_addr = paddr;
280160798Sjhb
281160798Sjhb	return (m);
282146806Srwatson}
283146806Srwatson
284160798Sjhbstatic void
285146806Srwatsondev_pager_putfake(m)
286160798Sjhb	vm_page_t m;
287146806Srwatson{
288160798Sjhb	if (!(m->flags & PG_FICTITIOUS))
289160798Sjhb		panic("dev_pager_putfake: bad page");
290160798Sjhb	zfree(fakepg_zone, m);
291146806Srwatson}
292160798Sjhb