device_pager.c revision 75675
150477Speter/*
295645Smarkm * Copyright (c) 1990 University of Utah.
32965Swollman * Copyright (c) 1991, 1993
42965Swollman *	The Regents of the University of California.  All rights reserved.
52965Swollman *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)device_pager.c	8.1 (Berkeley) 6/11/93
39 * $FreeBSD: head/sys/vm/device_pager.c 75675 2001-04-18 20:24:16Z alfred $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/mman.h>
46#include <sys/sx.h>
47
48#include <vm/vm.h>
49#include <vm/vm_object.h>
50#include <vm/vm_page.h>
51#include <vm/vm_pager.h>
52#include <vm/vm_zone.h>
53
54static void dev_pager_init __P((void));
55static vm_object_t dev_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t,
56		vm_ooffset_t));
57static void dev_pager_dealloc __P((vm_object_t));
58static int dev_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
59static void dev_pager_putpages __P((vm_object_t, vm_page_t *, int,
60		boolean_t, int *));
61static boolean_t dev_pager_haspage __P((vm_object_t, vm_pindex_t, int *,
62		int *));
63
64/* list of device pager objects */
65static struct pagerlst dev_pager_object_list;
66/* protect against object creation */
67static struct sx dev_pager_sx;
68/* protect list manipulation */
69static struct mtx dev_pager_mtx;
70
71
72static vm_zone_t fakepg_zone;
73static struct vm_zone fakepg_zone_store;
74
75static vm_page_t dev_pager_getfake __P((vm_offset_t));
76static void dev_pager_putfake __P((vm_page_t));
77
78struct pagerops devicepagerops = {
79	dev_pager_init,
80	dev_pager_alloc,
81	dev_pager_dealloc,
82	dev_pager_getpages,
83	dev_pager_putpages,
84	dev_pager_haspage,
85	NULL
86};
87
88static void
89dev_pager_init()
90{
91	TAILQ_INIT(&dev_pager_object_list);
92	sx_init(&dev_pager_sx, "dev_pager create");
93	mtx_init(&dev_pager_mtx, "dev_pager list", MTX_DEF);
94	fakepg_zone = &fakepg_zone_store;
95	zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2);
96}
97
98static vm_object_t
99dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
100{
101	dev_t dev;
102	d_mmap_t *mapfunc;
103	vm_object_t object;
104	unsigned int npages;
105	vm_offset_t off;
106
107	/*
108	 * Make sure this device can be mapped.
109	 */
110	dev = handle;
111	mapfunc = devsw(dev)->d_mmap;
112	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) {
113		printf("obsolete map function %p\n", (void *)mapfunc);
114		return (NULL);
115	}
116
117	/*
118	 * Offset should be page aligned.
119	 */
120	if (foff & PAGE_MASK)
121		return (NULL);
122
123	size = round_page(size);
124
125	/*
126	 * Check that the specified range of the device allows the desired
127	 * protection.
128	 *
129	 * XXX assumes VM_PROT_* == PROT_*
130	 */
131	npages = OFF_TO_IDX(size);
132	for (off = foff; npages--; off += PAGE_SIZE)
133		if ((*mapfunc) (dev, off, (int) prot) == -1)
134			return (NULL);
135
136	/*
137	 * Lock to prevent object creation race condition.
138	 */
139	sx_xlock(&dev_pager_sx);
140
141	/*
142	 * Look up pager, creating as necessary.
143	 */
144	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
145	if (object == NULL) {
146		/*
147		 * Allocate object and associate it with the pager.
148		 */
149		object = vm_object_allocate(OBJT_DEVICE,
150			OFF_TO_IDX(foff + size));
151		object->handle = handle;
152		TAILQ_INIT(&object->un_pager.devp.devp_pglist);
153		mtx_lock(&dev_pager_mtx);
154		TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
155		mtx_unlock(&dev_pager_mtx);
156	} else {
157		/*
158		 * Gain a reference to the object.
159		 */
160		vm_object_reference(object);
161		if (OFF_TO_IDX(foff + size) > object->size)
162			object->size = OFF_TO_IDX(foff + size);
163	}
164
165	sx_xunlock(&dev_pager_sx);
166
167	return (object);
168}
169
170static void
171dev_pager_dealloc(object)
172	vm_object_t object;
173{
174	vm_page_t m;
175
176	mtx_lock(&dev_pager_mtx);
177	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
178	mtx_unlock(&dev_pager_mtx);
179	/*
180	 * Free up our fake pages.
181	 */
182	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
183		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
184		dev_pager_putfake(m);
185	}
186}
187
188static int
189dev_pager_getpages(object, m, count, reqpage)
190	vm_object_t object;
191	vm_page_t *m;
192	int count;
193	int reqpage;
194{
195	vm_offset_t offset;
196	vm_offset_t paddr;
197	vm_page_t page;
198	dev_t dev;
199	int i, s;
200	d_mmap_t *mapfunc;
201	int prot;
202
203	dev = object->handle;
204	offset = m[reqpage]->pindex;
205	prot = PROT_READ;	/* XXX should pass in? */
206	mapfunc = devsw(dev)->d_mmap;
207
208	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop)
209		panic("dev_pager_getpage: no map function");
210
211	paddr = pmap_phys_address((*mapfunc) (dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
212	KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
213	/*
214	 * Replace the passed in reqpage page with our own fake page and free up the
215	 * all of the original pages.
216	 */
217	page = dev_pager_getfake(paddr);
218	TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
219	for (i = 0; i < count; i++) {
220		vm_page_free(m[i]);
221	}
222	s = splhigh();
223	vm_page_insert(page, object, offset);
224	splx(s);
225
226	return (VM_PAGER_OK);
227}
228
229static void
230dev_pager_putpages(object, m, count, sync, rtvals)
231	vm_object_t object;
232	vm_page_t *m;
233	int count;
234	boolean_t sync;
235	int *rtvals;
236{
237	panic("dev_pager_putpage called");
238}
239
240static boolean_t
241dev_pager_haspage(object, pindex, before, after)
242	vm_object_t object;
243	vm_pindex_t pindex;
244	int *before;
245	int *after;
246{
247	if (before != NULL)
248		*before = 0;
249	if (after != NULL)
250		*after = 0;
251	return (TRUE);
252}
253
254static vm_page_t
255dev_pager_getfake(paddr)
256	vm_offset_t paddr;
257{
258	vm_page_t m;
259
260	m = zalloc(fakepg_zone);
261
262	m->flags = PG_BUSY | PG_FICTITIOUS;
263	m->valid = VM_PAGE_BITS_ALL;
264	m->dirty = 0;
265	m->busy = 0;
266	m->queue = PQ_NONE;
267	m->object = NULL;
268
269	m->wire_count = 1;
270	m->hold_count = 0;
271	m->phys_addr = paddr;
272
273	return (m);
274}
275
276static void
277dev_pager_putfake(m)
278	vm_page_t m;
279{
280	if (!(m->flags & PG_FICTITIOUS))
281		panic("dev_pager_putfake: bad page");
282	zfree(fakepg_zone, m);
283}
284