2 * Copyright (c) 1990 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)device_pager.c 8.1 (Berkeley) 6/11/93
39 * $FreeBSD: src/sys/vm/device_pager.c,v 1.46.2.1 2000/08/02 21:54:37 peter Exp $
40 * $DragonFly: src/sys/vm/device_pager.c,v 1.2 2003/06/17 04:29:00 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.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>
54 static void dev_pager_init __P((void));
55 static vm_object_t dev_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t,
57 static void dev_pager_dealloc __P((vm_object_t));
58 static int dev_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
59 static void dev_pager_putpages __P((vm_object_t, vm_page_t *, int,
61 static boolean_t dev_pager_haspage __P((vm_object_t, vm_pindex_t, int *,
64 /* list of device pager objects */
65 static struct pagerlst dev_pager_object_list;
67 static vm_zone_t fakepg_zone;
68 static struct vm_zone fakepg_zone_store;
70 static vm_page_t dev_pager_getfake __P((vm_offset_t));
71 static void dev_pager_putfake __P((vm_page_t));
73 static int dev_pager_alloc_lock, dev_pager_alloc_lock_want;
75 struct pagerops devicepagerops = {
88 TAILQ_INIT(&dev_pager_object_list);
89 fakepg_zone = &fakepg_zone_store;
90 zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2);
94 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
103 * Make sure this device can be mapped.
106 mapfunc = devsw(dev)->d_mmap;
107 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) {
108 printf("obsolete map function %p\n", (void *)mapfunc);
113 * Offset should be page aligned.
115 if (foff & PAGE_MASK)
118 size = round_page(size);
121 * Check that the specified range of the device allows the desired
124 * XXX assumes VM_PROT_* == PROT_*
126 npages = OFF_TO_IDX(size);
127 for (off = foff; npages--; off += PAGE_SIZE)
128 if ((*mapfunc) (dev, off, (int) prot) == -1)
132 * Lock to prevent object creation race condition.
134 while (dev_pager_alloc_lock) {
135 dev_pager_alloc_lock_want++;
136 tsleep(&dev_pager_alloc_lock, PVM, "dvpall", 0);
137 dev_pager_alloc_lock_want--;
139 dev_pager_alloc_lock = 1;
142 * Look up pager, creating as necessary.
144 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
145 if (object == NULL) {
147 * Allocate object and associate it with the pager.
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 TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
156 * Gain a reference to the object.
158 vm_object_reference(object);
159 if (OFF_TO_IDX(foff + size) > object->size)
160 object->size = OFF_TO_IDX(foff + size);
163 dev_pager_alloc_lock = 0;
164 if (dev_pager_alloc_lock_want)
165 wakeup(&dev_pager_alloc_lock);
171 dev_pager_dealloc(object)
176 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
178 * Free up our fake pages.
180 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
181 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
182 dev_pager_putfake(m);
187 dev_pager_getpages(object, m, count, reqpage)
201 dev = object->handle;
202 offset = m[reqpage]->pindex;
203 prot = PROT_READ; /* XXX should pass in? */
204 mapfunc = devsw(dev)->d_mmap;
206 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop)
207 panic("dev_pager_getpage: no map function");
209 paddr = pmap_phys_address((*mapfunc) (dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
210 KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
212 * Replace the passed in reqpage page with our own fake page and free up the
213 * all of the original pages.
215 page = dev_pager_getfake(paddr);
216 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
217 for (i = 0; i < count; i++) {
221 vm_page_insert(page, object, offset);
224 return (VM_PAGER_OK);
228 dev_pager_putpages(object, m, count, sync, rtvals)
235 panic("dev_pager_putpage called");
239 dev_pager_haspage(object, pindex, before, after)
253 dev_pager_getfake(paddr)
258 m = zalloc(fakepg_zone);
260 m->flags = PG_BUSY | PG_FICTITIOUS;
261 m->valid = VM_PAGE_BITS_ALL;
269 m->phys_addr = paddr;
278 if (!(m->flags & PG_FICTITIOUS))
279 panic("dev_pager_putfake: bad page");
280 zfree(fakepg_zone, m);