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 19df8bae1dSRodney W. Grimes * 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 39aa8de40aSPoul-Henning Kamp * $Id: device_pager.c,v 1.21 1996/03/09 06:54:41 dyson Exp $ 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> 45df8bae1dSRodney W. Grimes #include <sys/mman.h> 46df8bae1dSRodney W. Grimes #include <sys/malloc.h> 47623ae52eSPoul-Henning Kamp #include <sys/proc.h> 48efeaf95aSDavid Greenman #include <sys/queue.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #include <vm/vm.h> 51efeaf95aSDavid Greenman #include <vm/vm_param.h> 52efeaf95aSDavid Greenman #include <vm/vm_prot.h> 53df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 54efeaf95aSDavid Greenman #include <vm/vm_object.h> 55df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 5624a1cce3SDavid Greenman #include <vm/vm_pager.h> 57df8bae1dSRodney W. Grimes #include <vm/device_pager.h> 58df8bae1dSRodney W. Grimes 59f708ef1bSPoul-Henning Kamp static void dev_pager_init __P((void)); 60f708ef1bSPoul-Henning Kamp static vm_object_t dev_pager_alloc __P((void *, vm_size_t, vm_prot_t, 61f708ef1bSPoul-Henning Kamp vm_ooffset_t)); 62f708ef1bSPoul-Henning Kamp static void dev_pager_dealloc __P((vm_object_t)); 63f708ef1bSPoul-Henning Kamp static int dev_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 64f708ef1bSPoul-Henning Kamp static int dev_pager_putpages __P((vm_object_t, vm_page_t *, int, 65f708ef1bSPoul-Henning Kamp boolean_t, int *)); 66f708ef1bSPoul-Henning Kamp static boolean_t dev_pager_haspage __P((vm_object_t, vm_pindex_t, int *, 67f708ef1bSPoul-Henning Kamp int *)); 68f708ef1bSPoul-Henning Kamp 69f708ef1bSPoul-Henning Kamp /* list of device pager objects */ 70f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list; 71f708ef1bSPoul-Henning Kamp 72f708ef1bSPoul-Henning Kamp /* list of available vm_page_t's */ 73f708ef1bSPoul-Henning Kamp static TAILQ_HEAD(, vm_page) dev_pager_fakelist; 74df8bae1dSRodney W. Grimes 75df8bae1dSRodney W. Grimes static vm_page_t dev_pager_getfake __P((vm_offset_t)); 76df8bae1dSRodney W. Grimes static void dev_pager_putfake __P((vm_page_t)); 77df8bae1dSRodney W. Grimes 7824a1cce3SDavid Greenman static int dev_pager_alloc_lock, dev_pager_alloc_lock_want; 7924a1cce3SDavid Greenman 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); 94df8bae1dSRodney W. Grimes TAILQ_INIT(&dev_pager_fakelist); 95df8bae1dSRodney W. Grimes } 96df8bae1dSRodney W. Grimes 97f708ef1bSPoul-Henning Kamp static vm_object_t 98df8bae1dSRodney W. Grimes dev_pager_alloc(handle, size, prot, foff) 99ee3a64c9SDavid Greenman void *handle; 100df8bae1dSRodney W. Grimes vm_size_t size; 101df8bae1dSRodney W. Grimes vm_prot_t prot; 102a316d390SJohn Dyson vm_ooffset_t foff; 103df8bae1dSRodney W. Grimes { 104df8bae1dSRodney W. Grimes dev_t dev; 105cac597e4SBruce Evans d_mmap_t *mapfunc; 106df8bae1dSRodney W. Grimes vm_object_t object; 10726f9a767SRodney W. Grimes unsigned int npages, off; 108df8bae1dSRodney W. Grimes 109df8bae1dSRodney W. Grimes /* 110df8bae1dSRodney W. Grimes * Make sure this device can be mapped. 111df8bae1dSRodney W. Grimes */ 11226f9a767SRodney W. Grimes dev = (dev_t) (u_long) handle; 1136ba9ebceSJulian Elischer mapfunc = cdevsw[major(dev)]->d_mmap; 114f31d402cSBruce Evans if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) { 115f31d402cSBruce Evans printf("obsolete map function %p\n", (void *)mapfunc); 116df8bae1dSRodney W. Grimes return (NULL); 117f31d402cSBruce Evans } 118df8bae1dSRodney W. Grimes 119df8bae1dSRodney W. Grimes /* 120df8bae1dSRodney W. Grimes * Offset should be page aligned. 121df8bae1dSRodney W. Grimes */ 122aa8de40aSPoul-Henning Kamp if (foff & PAGE_MASK) 123df8bae1dSRodney W. Grimes return (NULL); 124df8bae1dSRodney W. Grimes 125df8bae1dSRodney W. Grimes /* 1260d94caffSDavid Greenman * Check that the specified range of the device allows the desired 1270d94caffSDavid Greenman * protection. 128df8bae1dSRodney W. Grimes * 129df8bae1dSRodney W. Grimes * XXX assumes VM_PROT_* == PROT_* 130df8bae1dSRodney W. Grimes */ 131bd7e5f99SJohn Dyson npages = size; 132df8bae1dSRodney W. Grimes for (off = foff; npages--; off += PAGE_SIZE) 133df8bae1dSRodney W. Grimes if ((*mapfunc) (dev, off, (int) prot) == -1) 134df8bae1dSRodney W. Grimes return (NULL); 135df8bae1dSRodney W. Grimes 136df8bae1dSRodney W. Grimes /* 13724a1cce3SDavid Greenman * Lock to prevent object creation race contion. 13824a1cce3SDavid Greenman */ 13924a1cce3SDavid Greenman while (dev_pager_alloc_lock) { 14024a1cce3SDavid Greenman dev_pager_alloc_lock_want++; 14124a1cce3SDavid Greenman tsleep(&dev_pager_alloc_lock, PVM, "dvpall", 0); 14224a1cce3SDavid Greenman dev_pager_alloc_lock_want--; 14324a1cce3SDavid Greenman } 14424a1cce3SDavid Greenman dev_pager_alloc_lock = 1; 14524a1cce3SDavid Greenman 14624a1cce3SDavid Greenman /* 147df8bae1dSRodney W. Grimes * Look up pager, creating as necessary. 148df8bae1dSRodney W. Grimes */ 14924a1cce3SDavid Greenman object = vm_pager_object_lookup(&dev_pager_object_list, handle); 15024a1cce3SDavid Greenman if (object == NULL) { 151df8bae1dSRodney W. Grimes /* 152df8bae1dSRodney W. Grimes * Allocate object and associate it with the pager. 153df8bae1dSRodney W. Grimes */ 154a316d390SJohn Dyson object = vm_object_allocate(OBJT_DEVICE, 155bd7e5f99SJohn Dyson OFF_TO_IDX(foff) + size); 15624a1cce3SDavid Greenman object->handle = handle; 15724a1cce3SDavid Greenman TAILQ_INIT(&object->un_pager.devp.devp_pglist); 15824a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list); 159df8bae1dSRodney W. Grimes } else { 160df8bae1dSRodney W. Grimes /* 1617fb0c17eSDavid Greenman * Gain a reference to the object. 162df8bae1dSRodney W. Grimes */ 16324a1cce3SDavid Greenman vm_object_reference(object); 164bd7e5f99SJohn Dyson if (OFF_TO_IDX(foff) + size > object->size) 165bd7e5f99SJohn Dyson object->size = OFF_TO_IDX(foff) + size; 166df8bae1dSRodney W. Grimes } 167df8bae1dSRodney W. Grimes 16824a1cce3SDavid Greenman dev_pager_alloc_lock = 0; 16924a1cce3SDavid Greenman if (dev_pager_alloc_lock_want) 17024a1cce3SDavid Greenman wakeup(&dev_pager_alloc_lock); 17124a1cce3SDavid Greenman 17224a1cce3SDavid Greenman return (object); 17324a1cce3SDavid Greenman } 17424a1cce3SDavid Greenman 175f708ef1bSPoul-Henning Kamp static void 17624a1cce3SDavid Greenman dev_pager_dealloc(object) 177df8bae1dSRodney W. Grimes vm_object_t object; 17824a1cce3SDavid Greenman { 179df8bae1dSRodney W. Grimes vm_page_t m; 180df8bae1dSRodney W. Grimes 18124a1cce3SDavid Greenman TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 182df8bae1dSRodney W. Grimes /* 183df8bae1dSRodney W. Grimes * Free up our fake pages. 184df8bae1dSRodney W. Grimes */ 18524a1cce3SDavid Greenman while ((m = object->un_pager.devp.devp_pglist.tqh_first) != 0) { 18624a1cce3SDavid Greenman TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq); 187df8bae1dSRodney W. Grimes dev_pager_putfake(m); 188df8bae1dSRodney W. Grimes } 189df8bae1dSRodney W. Grimes } 190df8bae1dSRodney W. Grimes 191f708ef1bSPoul-Henning Kamp static int 19224a1cce3SDavid Greenman dev_pager_getpages(object, m, count, reqpage) 19324a1cce3SDavid Greenman vm_object_t object; 19424a1cce3SDavid Greenman vm_page_t *m; 19524a1cce3SDavid Greenman int count; 19624a1cce3SDavid Greenman int reqpage; 197df8bae1dSRodney W. Grimes { 198a316d390SJohn Dyson vm_offset_t offset; 199a316d390SJohn Dyson vm_offset_t paddr; 200df8bae1dSRodney W. Grimes vm_page_t page; 201df8bae1dSRodney W. Grimes dev_t dev; 20224a1cce3SDavid Greenman int i, s; 203cac597e4SBruce Evans d_mmap_t *mapfunc; 204cac597e4SBruce Evans int prot; 205df8bae1dSRodney W. Grimes 20624a1cce3SDavid Greenman dev = (dev_t) (u_long) object->handle; 207a316d390SJohn Dyson offset = m[reqpage]->pindex + OFF_TO_IDX(object->paging_offset); 208df8bae1dSRodney W. Grimes prot = PROT_READ; /* XXX should pass in? */ 2096ba9ebceSJulian Elischer mapfunc = cdevsw[major(dev)]->d_mmap; 21026f9a767SRodney W. Grimes 211f31d402cSBruce Evans if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) 212df8bae1dSRodney W. Grimes panic("dev_pager_getpage: no map function"); 21326f9a767SRodney W. Grimes 214a316d390SJohn Dyson paddr = pmap_phys_address((*mapfunc) ((dev_t) dev, (int) offset << PAGE_SHIFT, prot)); 215df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 216df8bae1dSRodney W. Grimes if (paddr == -1) 217df8bae1dSRodney W. Grimes panic("dev_pager_getpage: map function returns error"); 218df8bae1dSRodney W. Grimes #endif 219df8bae1dSRodney W. Grimes /* 22024a1cce3SDavid Greenman * Replace the passed in reqpage page with our own fake page and free up the 22124a1cce3SDavid Greenman * all of the original pages. 222df8bae1dSRodney W. Grimes */ 223df8bae1dSRodney W. Grimes page = dev_pager_getfake(paddr); 22424a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq); 22524a1cce3SDavid Greenman for (i = 0; i < count; i++) { 22624a1cce3SDavid Greenman PAGE_WAKEUP(m[i]); 22724a1cce3SDavid Greenman vm_page_free(m[i]); 22824a1cce3SDavid Greenman } 22926f9a767SRodney W. Grimes s = splhigh(); 23026f9a767SRodney W. Grimes vm_page_insert(page, object, offset); 23126f9a767SRodney W. Grimes splx(s); 232df8bae1dSRodney W. Grimes 233df8bae1dSRodney W. Grimes return (VM_PAGER_OK); 234df8bae1dSRodney W. Grimes } 235df8bae1dSRodney W. Grimes 236f708ef1bSPoul-Henning Kamp static int 23724a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals) 23824a1cce3SDavid Greenman vm_object_t object; 23924a1cce3SDavid Greenman vm_page_t *m; 24024a1cce3SDavid Greenman int count; 241df8bae1dSRodney W. Grimes boolean_t sync; 24224a1cce3SDavid Greenman int *rtvals; 243df8bae1dSRodney W. Grimes { 244df8bae1dSRodney W. Grimes panic("dev_pager_putpage called"); 245df8bae1dSRodney W. Grimes } 246df8bae1dSRodney W. Grimes 247f708ef1bSPoul-Henning Kamp static boolean_t 248a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after) 24924a1cce3SDavid Greenman vm_object_t object; 250a316d390SJohn Dyson vm_pindex_t pindex; 25124a1cce3SDavid Greenman int *before; 25224a1cce3SDavid Greenman int *after; 253df8bae1dSRodney W. Grimes { 25424a1cce3SDavid Greenman if (before != NULL) 25524a1cce3SDavid Greenman *before = 0; 25624a1cce3SDavid Greenman if (after != NULL) 25724a1cce3SDavid Greenman *after = 0; 258df8bae1dSRodney W. Grimes return (TRUE); 259df8bae1dSRodney W. Grimes } 260df8bae1dSRodney W. Grimes 261df8bae1dSRodney W. Grimes static vm_page_t 262df8bae1dSRodney W. Grimes dev_pager_getfake(paddr) 263df8bae1dSRodney W. Grimes vm_offset_t paddr; 264df8bae1dSRodney W. Grimes { 265df8bae1dSRodney W. Grimes vm_page_t m; 266df8bae1dSRodney W. Grimes int i; 267df8bae1dSRodney W. Grimes 268df8bae1dSRodney W. Grimes if (dev_pager_fakelist.tqh_first == NULL) { 2690d94caffSDavid Greenman m = (vm_page_t) malloc(PAGE_SIZE * 2, M_VMPGDATA, M_WAITOK); 2700d94caffSDavid Greenman for (i = (PAGE_SIZE * 2) / sizeof(*m); i > 0; i--) { 271df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&dev_pager_fakelist, m, pageq); 272df8bae1dSRodney W. Grimes m++; 273df8bae1dSRodney W. Grimes } 274df8bae1dSRodney W. Grimes } 275df8bae1dSRodney W. Grimes m = dev_pager_fakelist.tqh_first; 276df8bae1dSRodney W. Grimes TAILQ_REMOVE(&dev_pager_fakelist, m, pageq); 27726f9a767SRodney W. Grimes 2780d94caffSDavid Greenman m->flags = PG_BUSY | PG_FICTITIOUS; 2790d94caffSDavid Greenman m->valid = VM_PAGE_BITS_ALL; 28024a1cce3SDavid Greenman m->dirty = 0; 2810d94caffSDavid Greenman m->busy = 0; 282bd7e5f99SJohn Dyson m->queue = PQ_NONE; 28326f9a767SRodney W. Grimes 284df8bae1dSRodney W. Grimes m->wire_count = 1; 285c68f9c92SJohn Dyson m->hold_count = 0; 28626f9a767SRodney W. Grimes m->phys_addr = paddr; 28726f9a767SRodney W. Grimes 288df8bae1dSRodney W. Grimes return (m); 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes static void 292df8bae1dSRodney W. Grimes dev_pager_putfake(m) 293df8bae1dSRodney W. Grimes vm_page_t m; 294df8bae1dSRodney W. Grimes { 295df8bae1dSRodney W. Grimes if (!(m->flags & PG_FICTITIOUS)) 296df8bae1dSRodney W. Grimes panic("dev_pager_putfake: bad page"); 297df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&dev_pager_fakelist, m, pageq); 298df8bae1dSRodney W. Grimes } 299