1df8bae1dSRodney W. Grimes /* 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; 106cac597e4SBruce Evans d_mmap_t *mapfunc; 107df8bae1dSRodney W. Grimes vm_object_t object; 1082f7af3dbSAlan Cox vm_pindex_t pindex; 1097095ee91SDoug Rabson unsigned int npages; 110227f9a1cSJake Burkholder vm_paddr_t paddr; 111227f9a1cSJake Burkholder vm_offset_t off; 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; 126c8664f82SAlan Cox mtx_lock(&Giant); 127c8664f82SAlan Cox mapfunc = devsw(dev)->d_mmap; 128c8664f82SAlan Cox if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) { 129c8664f82SAlan Cox printf("obsolete map function %p\n", (void *)mapfunc); 130c8664f82SAlan Cox mtx_unlock(&Giant); 131c8664f82SAlan Cox return (NULL); 132c8664f82SAlan Cox } 133c8664f82SAlan Cox 134c8664f82SAlan Cox /* 1350d94caffSDavid Greenman * Check that the specified range of the device allows the desired 1360d94caffSDavid Greenman * protection. 137df8bae1dSRodney W. Grimes * 138df8bae1dSRodney W. Grimes * XXX assumes VM_PROT_* == PROT_* 139df8bae1dSRodney W. Grimes */ 1406cde7a16SDavid Greenman npages = OFF_TO_IDX(size); 141df8bae1dSRodney W. Grimes for (off = foff; npages--; off += PAGE_SIZE) 14207159f9cSMaxime Henrion if ((*mapfunc)(dev, off, &paddr, (int)prot) != 0) { 143c8664f82SAlan Cox mtx_unlock(&Giant); 144df8bae1dSRodney W. Grimes return (NULL); 145c8664f82SAlan Cox } 146df8bae1dSRodney W. Grimes 147df8bae1dSRodney W. Grimes /* 148956f3135SPhilippe Charnier * Lock to prevent object creation race condition. 14924a1cce3SDavid Greenman */ 150a9fa2c05SAlfred Perlstein sx_xlock(&dev_pager_sx); 15124a1cce3SDavid Greenman 15224a1cce3SDavid Greenman /* 153df8bae1dSRodney W. Grimes * Look up pager, creating as necessary. 154df8bae1dSRodney W. Grimes */ 15524a1cce3SDavid Greenman object = vm_pager_object_lookup(&dev_pager_object_list, handle); 15624a1cce3SDavid Greenman if (object == NULL) { 157df8bae1dSRodney W. Grimes /* 158df8bae1dSRodney W. Grimes * Allocate object and associate it with the pager. 159df8bae1dSRodney W. Grimes */ 1602f7af3dbSAlan Cox object = vm_object_allocate(OBJT_DEVICE, pindex); 16124a1cce3SDavid Greenman object->handle = handle; 16224a1cce3SDavid Greenman TAILQ_INIT(&object->un_pager.devp.devp_pglist); 163a9fa2c05SAlfred Perlstein mtx_lock(&dev_pager_mtx); 16424a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list); 165a9fa2c05SAlfred Perlstein mtx_unlock(&dev_pager_mtx); 166df8bae1dSRodney W. Grimes } else { 167df8bae1dSRodney W. Grimes /* 1687fb0c17eSDavid Greenman * Gain a reference to the object. 169df8bae1dSRodney W. Grimes */ 17024a1cce3SDavid Greenman vm_object_reference(object); 1712f7af3dbSAlan Cox if (pindex > object->size) 1722f7af3dbSAlan Cox object->size = pindex; 173df8bae1dSRodney W. Grimes } 174df8bae1dSRodney W. Grimes 175a9fa2c05SAlfred Perlstein sx_xunlock(&dev_pager_sx); 176c8664f82SAlan Cox mtx_unlock(&Giant); 17724a1cce3SDavid Greenman return (object); 17824a1cce3SDavid Greenman } 17924a1cce3SDavid Greenman 180f708ef1bSPoul-Henning Kamp static void 18124a1cce3SDavid Greenman dev_pager_dealloc(object) 182df8bae1dSRodney W. Grimes vm_object_t object; 18324a1cce3SDavid Greenman { 184df8bae1dSRodney W. Grimes vm_page_t m; 185df8bae1dSRodney W. Grimes 186a9fa2c05SAlfred Perlstein mtx_lock(&dev_pager_mtx); 18724a1cce3SDavid Greenman TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 188a9fa2c05SAlfred Perlstein mtx_unlock(&dev_pager_mtx); 189df8bae1dSRodney W. Grimes /* 190df8bae1dSRodney W. Grimes * Free up our fake pages. 191df8bae1dSRodney W. Grimes */ 192b18bfc3dSJohn Dyson while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) { 19324a1cce3SDavid Greenman TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq); 194df8bae1dSRodney W. Grimes dev_pager_putfake(m); 195df8bae1dSRodney W. Grimes } 196df8bae1dSRodney W. Grimes } 197df8bae1dSRodney W. Grimes 198f708ef1bSPoul-Henning Kamp static int 19924a1cce3SDavid Greenman dev_pager_getpages(object, m, count, reqpage) 20024a1cce3SDavid Greenman vm_object_t object; 20124a1cce3SDavid Greenman vm_page_t *m; 20224a1cce3SDavid Greenman int count; 20324a1cce3SDavid Greenman int reqpage; 204df8bae1dSRodney W. Grimes { 2056395da54SIan Dowse vm_pindex_t offset; 206227f9a1cSJake Burkholder vm_paddr_t paddr; 207df8bae1dSRodney W. Grimes vm_page_t page; 20889c9c53dSPoul-Henning Kamp struct cdev *dev; 20907159f9cSMaxime Henrion int i, ret; 210cac597e4SBruce Evans d_mmap_t *mapfunc; 211cac597e4SBruce Evans int prot; 212df8bae1dSRodney W. Grimes 213a8ab4870SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 214ebb4a317SBruce Evans dev = object->handle; 2151c7c3c6aSMatthew Dillon offset = m[reqpage]->pindex; 2168630c117SAlan Cox VM_OBJECT_UNLOCK(object); 21787aefa49SAlan Cox mtx_lock(&Giant); 218df8bae1dSRodney W. Grimes prot = PROT_READ; /* XXX should pass in? */ 2194be2eb8cSPoul-Henning Kamp mapfunc = devsw(dev)->d_mmap; 22026f9a767SRodney W. Grimes 221f31d402cSBruce Evans if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) 222df8bae1dSRodney W. Grimes panic("dev_pager_getpage: no map function"); 22326f9a767SRodney W. Grimes 22407159f9cSMaxime Henrion ret = (*mapfunc)(dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr, prot); 22507159f9cSMaxime Henrion KASSERT(ret == 0, ("dev_pager_getpage: map function returns error")); 22687aefa49SAlan Cox mtx_unlock(&Giant); 22787aefa49SAlan Cox 22892bab635SDoug Rabson if ((m[reqpage]->flags & PG_FICTITIOUS) != 0) { 22992bab635SDoug Rabson /* 23092bab635SDoug Rabson * If the passed in reqpage page is a fake page, update it with 23192bab635SDoug Rabson * the new physical address. 23292bab635SDoug Rabson */ 23392bab635SDoug Rabson dev_pager_updatefake(m[reqpage], paddr); 23492bab635SDoug Rabson VM_OBJECT_LOCK(object); 23592bab635SDoug Rabson if (count > 1) { 23692bab635SDoug Rabson vm_page_lock_queues(); 23792bab635SDoug Rabson for (i = 0; i < count; i++) { 23892bab635SDoug Rabson if (i != reqpage) 23992bab635SDoug Rabson vm_page_free(m[i]); 24092bab635SDoug Rabson } 24192bab635SDoug Rabson vm_page_unlock_queues(); 24292bab635SDoug Rabson } 24392bab635SDoug Rabson } else { 244df8bae1dSRodney W. Grimes /* 24507159f9cSMaxime Henrion * Replace the passed in reqpage page with our own fake page and 24607159f9cSMaxime Henrion * free up the all of the original pages. 247df8bae1dSRodney W. Grimes */ 248df8bae1dSRodney W. Grimes page = dev_pager_getfake(paddr); 2498630c117SAlan Cox VM_OBJECT_LOCK(object); 25024a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq); 25160e15726SAlan Cox vm_page_lock_queues(); 25260e15726SAlan Cox for (i = 0; i < count; i++) 25324a1cce3SDavid Greenman vm_page_free(m[i]); 25460e15726SAlan Cox vm_page_unlock_queues(); 25526f9a767SRodney W. Grimes vm_page_insert(page, object, offset); 2569aa3d17dSAlan Cox m[reqpage] = page; 25792bab635SDoug Rabson } 258df8bae1dSRodney W. Grimes 259df8bae1dSRodney W. Grimes return (VM_PAGER_OK); 260df8bae1dSRodney W. Grimes } 261df8bae1dSRodney W. Grimes 262e4542174SMatthew Dillon static void 26324a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals) 26424a1cce3SDavid Greenman vm_object_t object; 26524a1cce3SDavid Greenman vm_page_t *m; 26624a1cce3SDavid Greenman int count; 267df8bae1dSRodney W. Grimes boolean_t sync; 26824a1cce3SDavid Greenman int *rtvals; 269df8bae1dSRodney W. Grimes { 270df8bae1dSRodney W. Grimes panic("dev_pager_putpage called"); 271df8bae1dSRodney W. Grimes } 272df8bae1dSRodney W. Grimes 273f708ef1bSPoul-Henning Kamp static boolean_t 274a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after) 27524a1cce3SDavid Greenman vm_object_t object; 276a316d390SJohn Dyson vm_pindex_t pindex; 27724a1cce3SDavid Greenman int *before; 27824a1cce3SDavid Greenman int *after; 279df8bae1dSRodney W. Grimes { 28024a1cce3SDavid Greenman if (before != NULL) 28124a1cce3SDavid Greenman *before = 0; 28224a1cce3SDavid Greenman if (after != NULL) 28324a1cce3SDavid Greenman *after = 0; 284df8bae1dSRodney W. Grimes return (TRUE); 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes 287df8bae1dSRodney W. Grimes static vm_page_t 288df8bae1dSRodney W. Grimes dev_pager_getfake(paddr) 289227f9a1cSJake Burkholder vm_paddr_t paddr; 290df8bae1dSRodney W. Grimes { 291df8bae1dSRodney W. Grimes vm_page_t m; 292df8bae1dSRodney W. Grimes 293a163d034SWarner Losh m = uma_zalloc(fakepg_zone, M_WAITOK); 29426f9a767SRodney W. Grimes 2950d94caffSDavid Greenman m->flags = PG_BUSY | PG_FICTITIOUS; 2960d94caffSDavid Greenman m->valid = VM_PAGE_BITS_ALL; 29724a1cce3SDavid Greenman m->dirty = 0; 2980d94caffSDavid Greenman m->busy = 0; 299bd7e5f99SJohn Dyson m->queue = PQ_NONE; 300bb7db2c0SDavid Greenman m->object = NULL; 30126f9a767SRodney W. Grimes 302df8bae1dSRodney W. Grimes m->wire_count = 1; 303c68f9c92SJohn Dyson m->hold_count = 0; 30426f9a767SRodney W. Grimes m->phys_addr = paddr; 30526f9a767SRodney W. Grimes 306df8bae1dSRodney W. Grimes return (m); 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes 309df8bae1dSRodney W. Grimes static void 310df8bae1dSRodney W. Grimes dev_pager_putfake(m) 311df8bae1dSRodney W. Grimes vm_page_t m; 312df8bae1dSRodney W. Grimes { 313df8bae1dSRodney W. Grimes if (!(m->flags & PG_FICTITIOUS)) 314df8bae1dSRodney W. Grimes panic("dev_pager_putfake: bad page"); 315670d17b5SJeff Roberson uma_zfree(fakepg_zone, m); 316df8bae1dSRodney W. Grimes } 31792bab635SDoug Rabson 31892bab635SDoug Rabson static void 31992bab635SDoug Rabson dev_pager_updatefake(m, paddr) 32092bab635SDoug Rabson vm_page_t m; 32192bab635SDoug Rabson vm_paddr_t paddr; 32292bab635SDoug Rabson { 32392bab635SDoug Rabson if (!(m->flags & PG_FICTITIOUS)) 32492bab635SDoug Rabson panic("dev_pager_updatefake: bad page"); 32592bab635SDoug Rabson m->phys_addr = paddr; 32692bab635SDoug Rabson } 327