160727d8bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 3df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 4df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 7df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 8df8bae1dSRodney W. Grimes * Science Department. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 3426f9a767SRodney W. Grimes * @(#)device_pager.c 8.1 (Berkeley) 6/11/93 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37874651b1SDavid E. O'Brien #include <sys/cdefs.h> 38874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 39874651b1SDavid E. O'Brien 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 42df8bae1dSRodney W. Grimes #include <sys/conf.h> 43fb919e4dSMark Murray #include <sys/lock.h> 440cddd8f0SMatthew Dillon #include <sys/proc.h> 45fb919e4dSMark Murray #include <sys/mutex.h> 46df8bae1dSRodney W. Grimes #include <sys/mman.h> 47a9fa2c05SAlfred Perlstein #include <sys/sx.h> 48df8bae1dSRodney W. Grimes 49df8bae1dSRodney W. Grimes #include <vm/vm.h> 50efeaf95aSDavid Greenman #include <vm/vm_object.h> 51df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 5224a1cce3SDavid Greenman #include <vm/vm_pager.h> 53670d17b5SJeff Roberson #include <vm/uma.h> 54df8bae1dSRodney W. Grimes 5511caded3SAlfred Perlstein static void dev_pager_init(void); 5611caded3SAlfred Perlstein static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 5711caded3SAlfred Perlstein vm_ooffset_t); 5811caded3SAlfred Perlstein static void dev_pager_dealloc(vm_object_t); 5911caded3SAlfred Perlstein static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int); 6011caded3SAlfred Perlstein static void dev_pager_putpages(vm_object_t, vm_page_t *, int, 6111caded3SAlfred Perlstein boolean_t, int *); 6211caded3SAlfred Perlstein static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, 6311caded3SAlfred Perlstein int *); 64f708ef1bSPoul-Henning Kamp 65f708ef1bSPoul-Henning Kamp /* list of device pager objects */ 66f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list; 67a9fa2c05SAlfred Perlstein /* protect against object creation */ 68a9fa2c05SAlfred Perlstein static struct sx dev_pager_sx; 69a9fa2c05SAlfred Perlstein /* protect list manipulation */ 70a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx; 71a9fa2c05SAlfred Perlstein 72f708ef1bSPoul-Henning Kamp 73670d17b5SJeff Roberson static uma_zone_t fakepg_zone; 74df8bae1dSRodney W. Grimes 75227f9a1cSJake Burkholder static vm_page_t dev_pager_getfake(vm_paddr_t); 7611caded3SAlfred Perlstein static void dev_pager_putfake(vm_page_t); 7792bab635SDoug Rabson static void dev_pager_updatefake(vm_page_t, vm_paddr_t); 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes struct pagerops devicepagerops = { 804e658600SPoul-Henning Kamp .pgo_init = dev_pager_init, 814e658600SPoul-Henning Kamp .pgo_alloc = dev_pager_alloc, 824e658600SPoul-Henning Kamp .pgo_dealloc = dev_pager_dealloc, 834e658600SPoul-Henning Kamp .pgo_getpages = dev_pager_getpages, 844e658600SPoul-Henning Kamp .pgo_putpages = dev_pager_putpages, 854e658600SPoul-Henning Kamp .pgo_haspage = dev_pager_haspage, 86df8bae1dSRodney W. Grimes }; 87df8bae1dSRodney W. Grimes 88f708ef1bSPoul-Henning Kamp static void 89df8bae1dSRodney W. Grimes dev_pager_init() 90df8bae1dSRodney W. Grimes { 9124a1cce3SDavid Greenman TAILQ_INIT(&dev_pager_object_list); 92a9fa2c05SAlfred Perlstein sx_init(&dev_pager_sx, "dev_pager create"); 936008862bSJohn Baldwin mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF); 94670d17b5SJeff Roberson fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page), 95f3c625e4SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 96f3c625e4SJeff Roberson UMA_ZONE_NOFREE|UMA_ZONE_VM); 97df8bae1dSRodney W. Grimes } 98df8bae1dSRodney W. Grimes 99c8664f82SAlan Cox /* 100c8664f82SAlan Cox * MPSAFE 101c8664f82SAlan Cox */ 102f708ef1bSPoul-Henning Kamp static vm_object_t 1036cde7a16SDavid Greenman dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff) 104df8bae1dSRodney W. Grimes { 10589c9c53dSPoul-Henning Kamp struct cdev *dev; 106df8bae1dSRodney W. Grimes vm_object_t object; 1072f7af3dbSAlan Cox vm_pindex_t pindex; 1087095ee91SDoug Rabson unsigned int npages; 109227f9a1cSJake Burkholder vm_paddr_t paddr; 110227f9a1cSJake Burkholder vm_offset_t off; 111751fdd08SPoul-Henning Kamp struct cdevsw *csw; 112df8bae1dSRodney W. Grimes 113df8bae1dSRodney W. Grimes /* 114df8bae1dSRodney W. Grimes * Offset should be page aligned. 115df8bae1dSRodney W. Grimes */ 116aa8de40aSPoul-Henning Kamp if (foff & PAGE_MASK) 117df8bae1dSRodney W. Grimes return (NULL); 118df8bae1dSRodney W. Grimes 1196cde7a16SDavid Greenman size = round_page(size); 1202f7af3dbSAlan Cox pindex = OFF_TO_IDX(foff + size); 1216cde7a16SDavid Greenman 122df8bae1dSRodney W. Grimes /* 123c8664f82SAlan Cox * Make sure this device can be mapped. 124c8664f82SAlan Cox */ 125c8664f82SAlan Cox dev = handle; 126751fdd08SPoul-Henning Kamp csw = dev_refthread(dev); 127751fdd08SPoul-Henning Kamp if (csw == NULL) 128c8664f82SAlan Cox return (NULL); 129751fdd08SPoul-Henning Kamp mtx_lock(&Giant); 130c8664f82SAlan Cox 131c8664f82SAlan Cox /* 1320d94caffSDavid Greenman * Check that the specified range of the device allows the desired 1330d94caffSDavid Greenman * protection. 134df8bae1dSRodney W. Grimes * 135df8bae1dSRodney W. Grimes * XXX assumes VM_PROT_* == PROT_* 136df8bae1dSRodney W. Grimes */ 1376cde7a16SDavid Greenman npages = OFF_TO_IDX(size); 138df8bae1dSRodney W. Grimes for (off = foff; npages--; off += PAGE_SIZE) 139751fdd08SPoul-Henning Kamp if ((*csw->d_mmap)(dev, off, &paddr, (int)prot) != 0) { 140c8664f82SAlan Cox mtx_unlock(&Giant); 141751fdd08SPoul-Henning Kamp dev_relthread(dev); 142df8bae1dSRodney W. Grimes return (NULL); 143c8664f82SAlan Cox } 144df8bae1dSRodney W. Grimes 145df8bae1dSRodney W. Grimes /* 146956f3135SPhilippe Charnier * Lock to prevent object creation race condition. 14724a1cce3SDavid Greenman */ 148a9fa2c05SAlfred Perlstein sx_xlock(&dev_pager_sx); 14924a1cce3SDavid Greenman 15024a1cce3SDavid Greenman /* 151df8bae1dSRodney W. Grimes * Look up pager, creating as necessary. 152df8bae1dSRodney W. Grimes */ 15324a1cce3SDavid Greenman object = vm_pager_object_lookup(&dev_pager_object_list, handle); 15424a1cce3SDavid Greenman if (object == NULL) { 155df8bae1dSRodney W. Grimes /* 156df8bae1dSRodney W. Grimes * Allocate object and associate it with the pager. 157df8bae1dSRodney W. Grimes */ 1582f7af3dbSAlan Cox object = vm_object_allocate(OBJT_DEVICE, pindex); 15924a1cce3SDavid Greenman object->handle = handle; 16024a1cce3SDavid Greenman TAILQ_INIT(&object->un_pager.devp.devp_pglist); 161a9fa2c05SAlfred Perlstein mtx_lock(&dev_pager_mtx); 16224a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list); 163a9fa2c05SAlfred Perlstein mtx_unlock(&dev_pager_mtx); 164df8bae1dSRodney W. Grimes } else { 165df8bae1dSRodney W. Grimes /* 1667fb0c17eSDavid Greenman * Gain a reference to the object. 167df8bae1dSRodney W. Grimes */ 16824a1cce3SDavid Greenman vm_object_reference(object); 1692f7af3dbSAlan Cox if (pindex > object->size) 1702f7af3dbSAlan Cox object->size = pindex; 171df8bae1dSRodney W. Grimes } 172df8bae1dSRodney W. Grimes 173a9fa2c05SAlfred Perlstein sx_xunlock(&dev_pager_sx); 174c8664f82SAlan Cox mtx_unlock(&Giant); 175751fdd08SPoul-Henning Kamp dev_relthread(dev); 17624a1cce3SDavid Greenman return (object); 17724a1cce3SDavid Greenman } 17824a1cce3SDavid Greenman 179f708ef1bSPoul-Henning Kamp static void 18024a1cce3SDavid Greenman dev_pager_dealloc(object) 181df8bae1dSRodney W. Grimes vm_object_t object; 18224a1cce3SDavid Greenman { 183df8bae1dSRodney W. Grimes vm_page_t m; 184df8bae1dSRodney W. Grimes 185a9fa2c05SAlfred Perlstein mtx_lock(&dev_pager_mtx); 18624a1cce3SDavid Greenman TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 187a9fa2c05SAlfred Perlstein mtx_unlock(&dev_pager_mtx); 188df8bae1dSRodney W. Grimes /* 189df8bae1dSRodney W. Grimes * Free up our fake pages. 190df8bae1dSRodney W. Grimes */ 191b18bfc3dSJohn Dyson while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) { 19224a1cce3SDavid Greenman TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq); 193df8bae1dSRodney W. Grimes dev_pager_putfake(m); 194df8bae1dSRodney W. Grimes } 195df8bae1dSRodney W. Grimes } 196df8bae1dSRodney W. Grimes 197f708ef1bSPoul-Henning Kamp static int 19824a1cce3SDavid Greenman dev_pager_getpages(object, m, count, reqpage) 19924a1cce3SDavid Greenman vm_object_t object; 20024a1cce3SDavid Greenman vm_page_t *m; 20124a1cce3SDavid Greenman int count; 20224a1cce3SDavid Greenman int reqpage; 203df8bae1dSRodney W. Grimes { 2046395da54SIan Dowse vm_pindex_t offset; 205227f9a1cSJake Burkholder vm_paddr_t paddr; 206df8bae1dSRodney W. Grimes vm_page_t page; 20789c9c53dSPoul-Henning Kamp struct cdev *dev; 20807159f9cSMaxime Henrion int i, ret; 209cac597e4SBruce Evans int prot; 210751fdd08SPoul-Henning Kamp struct cdevsw *csw; 211df8bae1dSRodney W. Grimes 212a8ab4870SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 213ebb4a317SBruce Evans dev = object->handle; 2141c7c3c6aSMatthew Dillon offset = m[reqpage]->pindex; 2158630c117SAlan Cox VM_OBJECT_UNLOCK(object); 216751fdd08SPoul-Henning Kamp csw = dev_refthread(dev); 217751fdd08SPoul-Henning Kamp if (csw == NULL) 218751fdd08SPoul-Henning Kamp panic("dev_pager_getpage: no cdevsw"); 21987aefa49SAlan Cox mtx_lock(&Giant); 220df8bae1dSRodney W. Grimes prot = PROT_READ; /* XXX should pass in? */ 22126f9a767SRodney W. Grimes 222751fdd08SPoul-Henning Kamp ret = (*csw->d_mmap)(dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr, prot); 22307159f9cSMaxime Henrion KASSERT(ret == 0, ("dev_pager_getpage: map function returns error")); 22487aefa49SAlan Cox mtx_unlock(&Giant); 225751fdd08SPoul-Henning Kamp dev_relthread(dev); 22687aefa49SAlan Cox 22792bab635SDoug Rabson if ((m[reqpage]->flags & PG_FICTITIOUS) != 0) { 22892bab635SDoug Rabson /* 22992bab635SDoug Rabson * If the passed in reqpage page is a fake page, update it with 23092bab635SDoug Rabson * the new physical address. 23192bab635SDoug Rabson */ 23292bab635SDoug Rabson VM_OBJECT_LOCK(object); 233c413d99cSDoug Rabson dev_pager_updatefake(m[reqpage], paddr); 23492bab635SDoug Rabson if (count > 1) { 23592bab635SDoug Rabson vm_page_lock_queues(); 23692bab635SDoug Rabson for (i = 0; i < count; i++) { 23792bab635SDoug Rabson if (i != reqpage) 23892bab635SDoug Rabson vm_page_free(m[i]); 23992bab635SDoug Rabson } 24092bab635SDoug Rabson vm_page_unlock_queues(); 24192bab635SDoug Rabson } 24292bab635SDoug Rabson } else { 243df8bae1dSRodney W. Grimes /* 24407159f9cSMaxime Henrion * Replace the passed in reqpage page with our own fake page and 24507159f9cSMaxime Henrion * free up the all of the original pages. 246df8bae1dSRodney W. Grimes */ 247df8bae1dSRodney W. Grimes page = dev_pager_getfake(paddr); 2488630c117SAlan Cox VM_OBJECT_LOCK(object); 24924a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq); 25060e15726SAlan Cox vm_page_lock_queues(); 25160e15726SAlan Cox for (i = 0; i < count; i++) 25224a1cce3SDavid Greenman vm_page_free(m[i]); 25360e15726SAlan Cox vm_page_unlock_queues(); 25426f9a767SRodney W. Grimes vm_page_insert(page, object, offset); 2559aa3d17dSAlan Cox m[reqpage] = page; 25692bab635SDoug Rabson } 257df8bae1dSRodney W. Grimes 258df8bae1dSRodney W. Grimes return (VM_PAGER_OK); 259df8bae1dSRodney W. Grimes } 260df8bae1dSRodney W. Grimes 261e4542174SMatthew Dillon static void 26224a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals) 26324a1cce3SDavid Greenman vm_object_t object; 26424a1cce3SDavid Greenman vm_page_t *m; 26524a1cce3SDavid Greenman int count; 266df8bae1dSRodney W. Grimes boolean_t sync; 26724a1cce3SDavid Greenman int *rtvals; 268df8bae1dSRodney W. Grimes { 269df8bae1dSRodney W. Grimes panic("dev_pager_putpage called"); 270df8bae1dSRodney W. Grimes } 271df8bae1dSRodney W. Grimes 272f708ef1bSPoul-Henning Kamp static boolean_t 273a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after) 27424a1cce3SDavid Greenman vm_object_t object; 275a316d390SJohn Dyson vm_pindex_t pindex; 27624a1cce3SDavid Greenman int *before; 27724a1cce3SDavid Greenman int *after; 278df8bae1dSRodney W. Grimes { 27924a1cce3SDavid Greenman if (before != NULL) 28024a1cce3SDavid Greenman *before = 0; 28124a1cce3SDavid Greenman if (after != NULL) 28224a1cce3SDavid Greenman *after = 0; 283df8bae1dSRodney W. Grimes return (TRUE); 284df8bae1dSRodney W. Grimes } 285df8bae1dSRodney W. Grimes 286df8bae1dSRodney W. Grimes static vm_page_t 287df8bae1dSRodney W. Grimes dev_pager_getfake(paddr) 288227f9a1cSJake Burkholder vm_paddr_t paddr; 289df8bae1dSRodney W. Grimes { 290df8bae1dSRodney W. Grimes vm_page_t m; 291df8bae1dSRodney W. Grimes 292a163d034SWarner Losh m = uma_zalloc(fakepg_zone, M_WAITOK); 29326f9a767SRodney W. Grimes 2940d94caffSDavid Greenman m->flags = PG_BUSY | PG_FICTITIOUS; 2950d94caffSDavid Greenman m->valid = VM_PAGE_BITS_ALL; 29624a1cce3SDavid Greenman m->dirty = 0; 2970d94caffSDavid Greenman m->busy = 0; 298bd7e5f99SJohn Dyson m->queue = PQ_NONE; 299bb7db2c0SDavid Greenman m->object = NULL; 30026f9a767SRodney W. Grimes 301df8bae1dSRodney W. Grimes m->wire_count = 1; 302c68f9c92SJohn Dyson m->hold_count = 0; 30326f9a767SRodney W. Grimes m->phys_addr = paddr; 30426f9a767SRodney W. Grimes 305df8bae1dSRodney W. Grimes return (m); 306df8bae1dSRodney W. Grimes } 307df8bae1dSRodney W. Grimes 308df8bae1dSRodney W. Grimes static void 309df8bae1dSRodney W. Grimes dev_pager_putfake(m) 310df8bae1dSRodney W. Grimes vm_page_t m; 311df8bae1dSRodney W. Grimes { 312df8bae1dSRodney W. Grimes if (!(m->flags & PG_FICTITIOUS)) 313df8bae1dSRodney W. Grimes panic("dev_pager_putfake: bad page"); 314670d17b5SJeff Roberson uma_zfree(fakepg_zone, m); 315df8bae1dSRodney W. Grimes } 31692bab635SDoug Rabson 31792bab635SDoug Rabson static void 31892bab635SDoug Rabson dev_pager_updatefake(m, paddr) 31992bab635SDoug Rabson vm_page_t m; 32092bab635SDoug Rabson vm_paddr_t paddr; 32192bab635SDoug Rabson { 32292bab635SDoug Rabson if (!(m->flags & PG_FICTITIOUS)) 32392bab635SDoug Rabson panic("dev_pager_updatefake: bad page"); 32492bab635SDoug Rabson m->phys_addr = paddr; 325c413d99cSDoug Rabson m->valid = VM_PAGE_BITS_ALL; 32692bab635SDoug Rabson } 327