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;
71 static uma_zone_t fakepg_zone;
73 static vm_page_t dev_pager_getfake(vm_paddr_t, vm_memattr_t);
74 static void dev_pager_putfake(vm_page_t);
75 static void dev_pager_updatefake(vm_page_t, vm_paddr_t, vm_memattr_t);
77 struct pagerops devicepagerops = {
78 .pgo_init = dev_pager_init,
79 .pgo_alloc = dev_pager_alloc,
80 .pgo_dealloc = dev_pager_dealloc,
81 .pgo_getpages = dev_pager_getpages,
82 .pgo_putpages = dev_pager_putpages,
83 .pgo_haspage = dev_pager_haspage,
89 TAILQ_INIT(&dev_pager_object_list);
90 mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
91 fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page),
92 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
93 UMA_ZONE_NOFREE|UMA_ZONE_VM);
97 dev_mmap(struct cdevsw *csw, struct cdev *dev, vm_offset_t offset,
98 vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
101 if (csw->d_flags & D_MMAP2)
102 return (csw->d_mmap2(dev, offset, paddr, nprot, memattr));
104 return (csw->d_mmap(dev, offset, paddr, nprot));
111 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
112 vm_ooffset_t foff, struct ucred *cred)
115 vm_object_t object, object1;
124 * Offset should be page aligned.
126 if (foff & PAGE_MASK)
129 size = round_page(size);
130 pindex = OFF_TO_IDX(foff + size);
133 * Make sure this device can be mapped.
136 csw = dev_refthread(dev);
141 * Check that the specified range of the device allows the desired
144 * XXX assumes VM_PROT_* == PROT_*
146 npages = OFF_TO_IDX(size);
147 for (off = foff; npages--; off += PAGE_SIZE)
148 if (dev_mmap(csw, dev, off, &paddr, (int)prot, &dummy) != 0) {
153 mtx_lock(&dev_pager_mtx);
156 * Look up pager, creating as necessary.
159 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
160 if (object == NULL) {
162 * Allocate object and associate it with the pager. Initialize
163 * the object's pg_color based upon the physical address of the
166 mtx_unlock(&dev_pager_mtx);
167 object1 = vm_object_allocate(OBJT_DEVICE, pindex);
168 object1->flags |= OBJ_COLORED;
169 object1->pg_color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
170 mtx_lock(&dev_pager_mtx);
171 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
172 if (object != NULL) {
174 * We raced with other thread while allocating object.
176 if (pindex > object->size)
177 object->size = pindex;
181 object->handle = handle;
182 TAILQ_INIT(&object->un_pager.devp.devp_pglist);
183 TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
187 if (pindex > object->size)
188 object->size = pindex;
190 mtx_unlock(&dev_pager_mtx);
192 vm_object_deallocate(object1);
197 dev_pager_dealloc(object)
202 VM_OBJECT_UNLOCK(object);
203 mtx_lock(&dev_pager_mtx);
204 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
205 mtx_unlock(&dev_pager_mtx);
206 VM_OBJECT_LOCK(object);
208 * Free up our fake pages.
210 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != NULL) {
211 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
212 dev_pager_putfake(m);
217 dev_pager_getpages(object, m, count, reqpage)
225 vm_page_t m_paddr, page;
226 vm_memattr_t memattr;
233 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
234 dev = object->handle;
236 offset = page->pindex;
237 memattr = object->memattr;
238 VM_OBJECT_UNLOCK(object);
239 csw = dev_refthread(dev);
241 panic("dev_pager_getpage: no cdevsw");
245 ret = dev_mmap(csw, dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr,
246 PROT_READ, &memattr);
247 KASSERT(ret == 0, ("dev_pager_getpage: map function returns error"));
250 /* If "paddr" is a real page, perform a sanity check on "memattr". */
251 if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
252 pmap_page_get_memattr(m_paddr) != memattr) {
253 memattr = pmap_page_get_memattr(m_paddr);
255 "WARNING: A device driver has set \"memattr\" inconsistently.\n");
257 if ((page->flags & PG_FICTITIOUS) != 0) {
259 * If the passed in reqpage page is a fake page, update it with
260 * the new physical address.
262 VM_OBJECT_LOCK(object);
263 dev_pager_updatefake(page, paddr, memattr);
265 vm_page_lock_queues();
266 for (i = 0; i < count; i++) {
270 vm_page_unlock_queues();
274 * Replace the passed in reqpage page with our own fake page and
275 * free up the all of the original pages.
277 page = dev_pager_getfake(paddr, memattr);
278 VM_OBJECT_LOCK(object);
279 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
280 vm_page_lock_queues();
281 for (i = 0; i < count; i++)
283 vm_page_unlock_queues();
284 vm_page_insert(page, object, offset);
287 page->valid = VM_PAGE_BITS_ALL;
288 return (VM_PAGER_OK);
292 dev_pager_putpages(object, m, count, sync, rtvals)
299 panic("dev_pager_putpage called");
303 dev_pager_haspage(object, pindex, before, after)
317 * Create a fictitious page with the specified physical address and memory
318 * attribute. The memory attribute is the only the machine-dependent aspect
319 * of a fictitious page that must be initialized.
322 dev_pager_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
326 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
327 m->phys_addr = paddr;
328 /* Fictitious pages don't use "segind". */
329 m->flags = PG_FICTITIOUS;
330 /* Fictitious pages don't use "order" or "pool". */
331 m->oflags = VPO_BUSY;
333 pmap_page_set_memattr(m, memattr);
338 * Release a fictitious page.
341 dev_pager_putfake(vm_page_t m)
344 if (!(m->flags & PG_FICTITIOUS))
345 panic("dev_pager_putfake: bad page");
346 uma_zfree(fakepg_zone, m);
350 * Update the given fictitious page to the specified physical address and
354 dev_pager_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
357 if (!(m->flags & PG_FICTITIOUS))
358 panic("dev_pager_updatefake: bad page");
359 m->phys_addr = paddr;
360 pmap_page_set_memattr(m, memattr);