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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)device_pager.c 8.1 (Berkeley) 6/11/93
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
45 #include <sys/mutex.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_pager.h>
55 static void dev_pager_init(void);
56 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
57 vm_ooffset_t, struct ucred *);
58 static void dev_pager_dealloc(vm_object_t);
59 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int);
60 static void dev_pager_putpages(vm_object_t, vm_page_t *, int,
62 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *,
65 /* list of device pager objects */
66 static struct pagerlst dev_pager_object_list;
67 /* protect list manipulation */
68 static struct mtx dev_pager_mtx;
70 struct pagerops devicepagerops = {
71 .pgo_init = dev_pager_init,
72 .pgo_alloc = dev_pager_alloc,
73 .pgo_dealloc = dev_pager_dealloc,
74 .pgo_getpages = dev_pager_getpages,
75 .pgo_putpages = dev_pager_putpages,
76 .pgo_haspage = dev_pager_haspage,
79 static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
80 vm_ooffset_t foff, struct ucred *cred, u_short *color);
81 static void old_dev_pager_dtor(void *handle);
82 static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
83 int prot, vm_page_t *mres);
85 static struct cdev_pager_ops old_dev_pager_ops = {
86 .cdev_pg_ctor = old_dev_pager_ctor,
87 .cdev_pg_dtor = old_dev_pager_dtor,
88 .cdev_pg_fault = old_dev_pager_fault
94 TAILQ_INIT(&dev_pager_object_list);
95 mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
99 cdev_pager_lookup(void *handle)
103 mtx_lock(&dev_pager_mtx);
104 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
105 vm_object_reference(object);
106 mtx_unlock(&dev_pager_mtx);
111 cdev_pager_allocate(void *handle, enum obj_type tp, struct cdev_pager_ops *ops,
112 vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred)
114 vm_object_t object, object1;
118 if (tp != OBJT_DEVICE)
122 * Offset should be page aligned.
124 if (foff & PAGE_MASK)
127 size = round_page(size);
128 pindex = OFF_TO_IDX(foff + size);
130 if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)
132 mtx_lock(&dev_pager_mtx);
135 * Look up pager, creating as necessary.
138 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
139 if (object == NULL) {
141 * Allocate object and associate it with the pager. Initialize
142 * the object's pg_color based upon the physical address of the
145 mtx_unlock(&dev_pager_mtx);
146 object1 = vm_object_allocate(tp, pindex);
147 object1->flags |= OBJ_COLORED;
148 object1->pg_color = color;
149 object1->handle = handle;
150 object1->un_pager.devp.ops = ops;
151 TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
152 mtx_lock(&dev_pager_mtx);
153 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
154 if (object != NULL) {
156 * We raced with other thread while allocating object.
158 if (pindex > object->size)
159 object->size = pindex;
163 object->handle = handle;
164 TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
166 KASSERT(object->type == tp,
167 ("Inconsistent device pager type %p %d", object, tp));
170 if (pindex > object->size)
171 object->size = pindex;
173 mtx_unlock(&dev_pager_mtx);
174 if (object1 != NULL) {
175 object1->handle = object1;
176 mtx_lock(&dev_pager_mtx);
177 TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
179 mtx_unlock(&dev_pager_mtx);
180 vm_object_deallocate(object1);
186 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
187 vm_ooffset_t foff, struct ucred *cred)
190 return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
191 size, prot, foff, cred));
195 cdev_pager_free_page(vm_object_t object, vm_page_t m)
198 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
199 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
204 dev_pager_dealloc(object)
209 VM_OBJECT_UNLOCK(object);
210 object->un_pager.devp.ops->cdev_pg_dtor(object->handle);
212 mtx_lock(&dev_pager_mtx);
213 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
214 mtx_unlock(&dev_pager_mtx);
215 VM_OBJECT_LOCK(object);
217 * Free up our fake pages.
219 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != NULL)
220 cdev_pager_free_page(object, m);
224 dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int reqpage)
228 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
229 error = object->un_pager.devp.ops->cdev_pg_fault(object,
230 IDX_TO_OFF(ma[reqpage]->pindex), PROT_READ, &ma[reqpage]);
232 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
234 for (i = 0; i < count; i++) {
238 vm_page_unlock(ma[i]);
242 if (error == VM_PAGER_OK) {
243 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
251 old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
256 vm_page_t m_paddr, page;
261 vm_memattr_t memattr;
264 pidx = OFF_TO_IDX(offset);
265 memattr = object->memattr;
267 VM_OBJECT_UNLOCK(object);
269 dev = object->handle;
270 csw = dev_refthread(dev, &ref);
272 VM_OBJECT_LOCK(object);
273 return (VM_PAGER_FAIL);
278 ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
280 dev_relthread(dev, ref);
283 "WARNING: dev_pager_getpage: map function returns error %d", ret);
284 VM_OBJECT_LOCK(object);
285 return (VM_PAGER_FAIL);
288 /* If "paddr" is a real page, perform a sanity check on "memattr". */
289 if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
290 pmap_page_get_memattr(m_paddr) != memattr) {
291 memattr = pmap_page_get_memattr(m_paddr);
293 "WARNING: A device driver has set \"memattr\" inconsistently.\n");
295 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
297 * If the passed in result page is a fake page, update it with
298 * the new physical address.
301 VM_OBJECT_LOCK(object);
302 vm_page_updatefake(page, paddr, memattr);
305 * Replace the passed in reqpage page with our own fake page and
306 * free up the all of the original pages.
308 page = vm_page_getfake(paddr, memattr);
309 VM_OBJECT_LOCK(object);
312 vm_page_unlock(*mres);
314 vm_page_insert(page, object, pidx);
316 page->valid = VM_PAGE_BITS_ALL;
317 return (VM_PAGER_OK);
321 dev_pager_putpages(object, m, count, sync, rtvals)
329 panic("dev_pager_putpage called");
333 dev_pager_haspage(object, pindex, before, after)
347 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
348 vm_ooffset_t foff, struct ucred *cred, u_short *color)
359 * Make sure this device can be mapped.
362 csw = dev_refthread(dev, &ref);
367 * Check that the specified range of the device allows the desired
370 * XXX assumes VM_PROT_* == PROT_*
372 npages = OFF_TO_IDX(size);
373 for (off = foff; npages--; off += PAGE_SIZE) {
374 if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
375 dev_relthread(dev, ref);
381 dev_relthread(dev, ref);
382 *color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
387 old_dev_pager_dtor(void *handle)