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 * 3. All advertising materials mentioning features or use of this software 195929bcfaSPhilippe Charnier * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 3826f9a767SRodney W. Grimes * @(#)device_pager.c 8.1 (Berkeley) 6/11/93 39c3aac50fSPeter Wemm * $FreeBSD$ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44df8bae1dSRodney W. Grimes #include <sys/conf.h> 45fb919e4dSMark Murray #include <sys/lock.h> 460cddd8f0SMatthew Dillon #include <sys/proc.h> 47fb919e4dSMark Murray #include <sys/mutex.h> 48df8bae1dSRodney W. Grimes #include <sys/mman.h> 49a9fa2c05SAlfred Perlstein #include <sys/sx.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <vm/vm.h> 52efeaf95aSDavid Greenman #include <vm/vm_object.h> 53df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 5424a1cce3SDavid Greenman #include <vm/vm_pager.h> 55670d17b5SJeff Roberson #include <vm/uma.h> 56df8bae1dSRodney W. Grimes 5711caded3SAlfred Perlstein static void dev_pager_init(void); 5811caded3SAlfred Perlstein static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 5911caded3SAlfred Perlstein vm_ooffset_t); 6011caded3SAlfred Perlstein static void dev_pager_dealloc(vm_object_t); 6111caded3SAlfred Perlstein static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int); 6211caded3SAlfred Perlstein static void dev_pager_putpages(vm_object_t, vm_page_t *, int, 6311caded3SAlfred Perlstein boolean_t, int *); 6411caded3SAlfred Perlstein static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, 6511caded3SAlfred Perlstein int *); 66f708ef1bSPoul-Henning Kamp 67f708ef1bSPoul-Henning Kamp /* list of device pager objects */ 68f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list; 69a9fa2c05SAlfred Perlstein /* protect against object creation */ 70a9fa2c05SAlfred Perlstein static struct sx dev_pager_sx; 71a9fa2c05SAlfred Perlstein /* protect list manipulation */ 72a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx; 73a9fa2c05SAlfred Perlstein 74f708ef1bSPoul-Henning Kamp 75670d17b5SJeff Roberson static uma_zone_t fakepg_zone; 76df8bae1dSRodney W. Grimes 7711caded3SAlfred Perlstein static vm_page_t dev_pager_getfake(vm_offset_t); 7811caded3SAlfred Perlstein static void dev_pager_putfake(vm_page_t); 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes struct pagerops devicepagerops = { 81df8bae1dSRodney W. Grimes dev_pager_init, 82df8bae1dSRodney W. Grimes dev_pager_alloc, 83df8bae1dSRodney W. Grimes dev_pager_dealloc, 8424a1cce3SDavid Greenman dev_pager_getpages, 8524a1cce3SDavid Greenman dev_pager_putpages, 8624a1cce3SDavid Greenman dev_pager_haspage, 8724a1cce3SDavid Greenman NULL 88df8bae1dSRodney W. Grimes }; 89df8bae1dSRodney W. Grimes 90f708ef1bSPoul-Henning Kamp static void 91df8bae1dSRodney W. Grimes dev_pager_init() 92df8bae1dSRodney W. Grimes { 9324a1cce3SDavid Greenman TAILQ_INIT(&dev_pager_object_list); 94a9fa2c05SAlfred Perlstein sx_init(&dev_pager_sx, "dev_pager create"); 956008862bSJohn Baldwin mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF); 96670d17b5SJeff Roberson fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page), 97670d17b5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 98df8bae1dSRodney W. Grimes } 99df8bae1dSRodney W. Grimes 100c8664f82SAlan Cox /* 101c8664f82SAlan Cox * MPSAFE 102c8664f82SAlan Cox */ 103f708ef1bSPoul-Henning Kamp static vm_object_t 1046cde7a16SDavid Greenman dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff) 105df8bae1dSRodney W. Grimes { 106df8bae1dSRodney W. Grimes dev_t dev; 107cac597e4SBruce Evans d_mmap_t *mapfunc; 108df8bae1dSRodney W. Grimes vm_object_t object; 1097095ee91SDoug Rabson unsigned int npages; 11007159f9cSMaxime Henrion vm_offset_t off, paddr; 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes /* 113df8bae1dSRodney W. Grimes * Offset should be page aligned. 114df8bae1dSRodney W. Grimes */ 115aa8de40aSPoul-Henning Kamp if (foff & PAGE_MASK) 116df8bae1dSRodney W. Grimes return (NULL); 117df8bae1dSRodney W. Grimes 1186cde7a16SDavid Greenman size = round_page(size); 1196cde7a16SDavid Greenman 120df8bae1dSRodney W. Grimes /* 121c8664f82SAlan Cox * Make sure this device can be mapped. 122c8664f82SAlan Cox */ 123c8664f82SAlan Cox dev = handle; 124c8664f82SAlan Cox mtx_lock(&Giant); 125c8664f82SAlan Cox mapfunc = devsw(dev)->d_mmap; 126c8664f82SAlan Cox if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) { 127c8664f82SAlan Cox printf("obsolete map function %p\n", (void *)mapfunc); 128c8664f82SAlan Cox mtx_unlock(&Giant); 129c8664f82SAlan Cox return (NULL); 130c8664f82SAlan Cox } 131c8664f82SAlan Cox 132c8664f82SAlan Cox /* 1330d94caffSDavid Greenman * Check that the specified range of the device allows the desired 1340d94caffSDavid Greenman * protection. 135df8bae1dSRodney W. Grimes * 136df8bae1dSRodney W. Grimes * XXX assumes VM_PROT_* == PROT_* 137df8bae1dSRodney W. Grimes */ 1386cde7a16SDavid Greenman npages = OFF_TO_IDX(size); 139df8bae1dSRodney W. Grimes for (off = foff; npages--; off += PAGE_SIZE) 14007159f9cSMaxime Henrion if ((*mapfunc)(dev, off, &paddr, (int)prot) != 0) { 141c8664f82SAlan Cox mtx_unlock(&Giant); 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 */ 158a316d390SJohn Dyson object = vm_object_allocate(OBJT_DEVICE, 1596cde7a16SDavid Greenman OFF_TO_IDX(foff + size)); 16024a1cce3SDavid Greenman object->handle = handle; 16124a1cce3SDavid Greenman TAILQ_INIT(&object->un_pager.devp.devp_pglist); 162a9fa2c05SAlfred Perlstein mtx_lock(&dev_pager_mtx); 16324a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list); 164a9fa2c05SAlfred Perlstein mtx_unlock(&dev_pager_mtx); 165df8bae1dSRodney W. Grimes } else { 166df8bae1dSRodney W. Grimes /* 1677fb0c17eSDavid Greenman * Gain a reference to the object. 168df8bae1dSRodney W. Grimes */ 16924a1cce3SDavid Greenman vm_object_reference(object); 1706cde7a16SDavid Greenman if (OFF_TO_IDX(foff + size) > object->size) 1716cde7a16SDavid Greenman object->size = OFF_TO_IDX(foff + size); 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes 174a9fa2c05SAlfred Perlstein sx_xunlock(&dev_pager_sx); 175c8664f82SAlan Cox mtx_unlock(&Giant); 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; 205a316d390SJohn Dyson vm_offset_t paddr; 206df8bae1dSRodney W. Grimes vm_page_t page; 207df8bae1dSRodney W. Grimes dev_t dev; 20807159f9cSMaxime Henrion int i, ret; 209cac597e4SBruce Evans d_mmap_t *mapfunc; 210cac597e4SBruce Evans int prot; 211df8bae1dSRodney W. Grimes 2126d556da5SJohn Baldwin mtx_assert(&Giant, MA_OWNED); 213ebb4a317SBruce Evans dev = object->handle; 2141c7c3c6aSMatthew Dillon offset = m[reqpage]->pindex; 215df8bae1dSRodney W. Grimes prot = PROT_READ; /* XXX should pass in? */ 2164be2eb8cSPoul-Henning Kamp mapfunc = devsw(dev)->d_mmap; 21726f9a767SRodney W. Grimes 218f31d402cSBruce Evans if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) 219df8bae1dSRodney W. Grimes panic("dev_pager_getpage: no map function"); 22026f9a767SRodney W. Grimes 22107159f9cSMaxime Henrion ret = (*mapfunc)(dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr, prot); 22207159f9cSMaxime Henrion KASSERT(ret == 0, ("dev_pager_getpage: map function returns error")); 223df8bae1dSRodney W. Grimes /* 22407159f9cSMaxime Henrion * Replace the passed in reqpage page with our own fake page and 22507159f9cSMaxime Henrion * free up the all of the original pages. 226df8bae1dSRodney W. Grimes */ 227df8bae1dSRodney W. Grimes page = dev_pager_getfake(paddr); 22824a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq); 22960e15726SAlan Cox vm_page_lock_queues(); 23060e15726SAlan Cox for (i = 0; i < count; i++) 23124a1cce3SDavid Greenman vm_page_free(m[i]); 23260e15726SAlan Cox vm_page_unlock_queues(); 23326f9a767SRodney W. Grimes vm_page_insert(page, object, offset); 234df8bae1dSRodney W. Grimes 235df8bae1dSRodney W. Grimes return (VM_PAGER_OK); 236df8bae1dSRodney W. Grimes } 237df8bae1dSRodney W. Grimes 238e4542174SMatthew Dillon static void 23924a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals) 24024a1cce3SDavid Greenman vm_object_t object; 24124a1cce3SDavid Greenman vm_page_t *m; 24224a1cce3SDavid Greenman int count; 243df8bae1dSRodney W. Grimes boolean_t sync; 24424a1cce3SDavid Greenman int *rtvals; 245df8bae1dSRodney W. Grimes { 246df8bae1dSRodney W. Grimes panic("dev_pager_putpage called"); 247df8bae1dSRodney W. Grimes } 248df8bae1dSRodney W. Grimes 249f708ef1bSPoul-Henning Kamp static boolean_t 250a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after) 25124a1cce3SDavid Greenman vm_object_t object; 252a316d390SJohn Dyson vm_pindex_t pindex; 25324a1cce3SDavid Greenman int *before; 25424a1cce3SDavid Greenman int *after; 255df8bae1dSRodney W. Grimes { 25624a1cce3SDavid Greenman if (before != NULL) 25724a1cce3SDavid Greenman *before = 0; 25824a1cce3SDavid Greenman if (after != NULL) 25924a1cce3SDavid Greenman *after = 0; 260df8bae1dSRodney W. Grimes return (TRUE); 261df8bae1dSRodney W. Grimes } 262df8bae1dSRodney W. Grimes 263df8bae1dSRodney W. Grimes static vm_page_t 264df8bae1dSRodney W. Grimes dev_pager_getfake(paddr) 265df8bae1dSRodney W. Grimes vm_offset_t paddr; 266df8bae1dSRodney W. Grimes { 267df8bae1dSRodney W. Grimes vm_page_t m; 268df8bae1dSRodney W. Grimes 269a163d034SWarner Losh m = uma_zalloc(fakepg_zone, M_WAITOK); 27026f9a767SRodney W. Grimes 2710d94caffSDavid Greenman m->flags = PG_BUSY | PG_FICTITIOUS; 2720d94caffSDavid Greenman m->valid = VM_PAGE_BITS_ALL; 27324a1cce3SDavid Greenman m->dirty = 0; 2740d94caffSDavid Greenman m->busy = 0; 275bd7e5f99SJohn Dyson m->queue = PQ_NONE; 276bb7db2c0SDavid Greenman m->object = NULL; 27726f9a767SRodney W. Grimes 278df8bae1dSRodney W. Grimes m->wire_count = 1; 279c68f9c92SJohn Dyson m->hold_count = 0; 28026f9a767SRodney W. Grimes m->phys_addr = paddr; 28126f9a767SRodney W. Grimes 282df8bae1dSRodney W. Grimes return (m); 283df8bae1dSRodney W. Grimes } 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes static void 286df8bae1dSRodney W. Grimes dev_pager_putfake(m) 287df8bae1dSRodney W. Grimes vm_page_t m; 288df8bae1dSRodney W. Grimes { 289df8bae1dSRodney W. Grimes if (!(m->flags & PG_FICTITIOUS)) 290df8bae1dSRodney W. Grimes panic("dev_pager_putfake: bad page"); 291670d17b5SJeff Roberson uma_zfree(fakepg_zone, m); 292df8bae1dSRodney W. Grimes } 293