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. 18fbbd9655SWarner Losh * 3. 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> 4789f6b863SAttilio Rao #include <sys/rwlock.h> 48a9fa2c05SAlfred Perlstein #include <sys/sx.h> 49*9ed01c32SGleb Smirnoff #include <sys/vmmeter.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <vm/vm.h> 521c771f92SKonstantin Belousov #include <vm/vm_param.h> 53efeaf95aSDavid Greenman #include <vm/vm_object.h> 54df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 5524a1cce3SDavid Greenman #include <vm/vm_pager.h> 5643f48b65SKonstantin Belousov #include <vm/vm_phys.h> 57670d17b5SJeff Roberson #include <vm/uma.h> 58df8bae1dSRodney W. Grimes 5911caded3SAlfred Perlstein static void dev_pager_init(void); 6011caded3SAlfred Perlstein static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 613364c323SKonstantin Belousov vm_ooffset_t, struct ucred *); 6211caded3SAlfred Perlstein static void dev_pager_dealloc(vm_object_t); 63b0cd2017SGleb Smirnoff static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *); 64b89def80SKonstantin Belousov static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 65b89def80SKonstantin Belousov static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 66b7ac5a85SKonstantin Belousov static void dev_pager_free_page(vm_object_t object, vm_page_t m); 67c42b43a0SKonstantin Belousov static int dev_pager_populate(vm_object_t object, vm_pindex_t pidx, 68c42b43a0SKonstantin Belousov int fault_type, vm_prot_t, vm_pindex_t *first, vm_pindex_t *last); 69f708ef1bSPoul-Henning Kamp 70f708ef1bSPoul-Henning Kamp /* list of device pager objects */ 71f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list; 72a9fa2c05SAlfred Perlstein /* protect list manipulation */ 73a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx; 74a9fa2c05SAlfred Perlstein 75df8bae1dSRodney W. Grimes struct pagerops devicepagerops = { 764e658600SPoul-Henning Kamp .pgo_init = dev_pager_init, 774e658600SPoul-Henning Kamp .pgo_alloc = dev_pager_alloc, 784e658600SPoul-Henning Kamp .pgo_dealloc = dev_pager_dealloc, 794e658600SPoul-Henning Kamp .pgo_getpages = dev_pager_getpages, 804e658600SPoul-Henning Kamp .pgo_putpages = dev_pager_putpages, 814e658600SPoul-Henning Kamp .pgo_haspage = dev_pager_haspage, 82df8bae1dSRodney W. Grimes }; 83df8bae1dSRodney W. Grimes 84b7ac5a85SKonstantin Belousov struct pagerops mgtdevicepagerops = { 85b7ac5a85SKonstantin Belousov .pgo_alloc = dev_pager_alloc, 86b7ac5a85SKonstantin Belousov .pgo_dealloc = dev_pager_dealloc, 87b7ac5a85SKonstantin Belousov .pgo_getpages = dev_pager_getpages, 88b7ac5a85SKonstantin Belousov .pgo_putpages = dev_pager_putpages, 89b7ac5a85SKonstantin Belousov .pgo_haspage = dev_pager_haspage, 90c42b43a0SKonstantin Belousov .pgo_populate = dev_pager_populate, 91b7ac5a85SKonstantin Belousov }; 92b7ac5a85SKonstantin Belousov 93286790a7SKonstantin Belousov static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 94286790a7SKonstantin Belousov vm_ooffset_t foff, struct ucred *cred, u_short *color); 95286790a7SKonstantin Belousov static void old_dev_pager_dtor(void *handle); 96286790a7SKonstantin Belousov static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, 97286790a7SKonstantin Belousov int prot, vm_page_t *mres); 98286790a7SKonstantin Belousov 99286790a7SKonstantin Belousov static struct cdev_pager_ops old_dev_pager_ops = { 100286790a7SKonstantin Belousov .cdev_pg_ctor = old_dev_pager_ctor, 101286790a7SKonstantin Belousov .cdev_pg_dtor = old_dev_pager_dtor, 102286790a7SKonstantin Belousov .cdev_pg_fault = old_dev_pager_fault 103286790a7SKonstantin Belousov }; 104286790a7SKonstantin Belousov 105f708ef1bSPoul-Henning Kamp static void 106b89def80SKonstantin Belousov dev_pager_init(void) 107df8bae1dSRodney W. Grimes { 108b89def80SKonstantin Belousov 10924a1cce3SDavid Greenman TAILQ_INIT(&dev_pager_object_list); 1106008862bSJohn Baldwin mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF); 111df8bae1dSRodney W. Grimes } 112df8bae1dSRodney W. Grimes 113286790a7SKonstantin Belousov vm_object_t 114286790a7SKonstantin Belousov cdev_pager_lookup(void *handle) 115df8bae1dSRodney W. Grimes { 116286790a7SKonstantin Belousov vm_object_t object; 117286790a7SKonstantin Belousov 118286790a7SKonstantin Belousov mtx_lock(&dev_pager_mtx); 119286790a7SKonstantin Belousov object = vm_pager_object_lookup(&dev_pager_object_list, handle); 120286790a7SKonstantin Belousov mtx_unlock(&dev_pager_mtx); 121286790a7SKonstantin Belousov return (object); 122286790a7SKonstantin Belousov } 123286790a7SKonstantin Belousov 124286790a7SKonstantin Belousov vm_object_t 125286790a7SKonstantin Belousov cdev_pager_allocate(void *handle, enum obj_type tp, struct cdev_pager_ops *ops, 126286790a7SKonstantin Belousov vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred) 127286790a7SKonstantin Belousov { 128deea654eSKonstantin Belousov vm_object_t object, object1; 1292f7af3dbSAlan Cox vm_pindex_t pindex; 130286790a7SKonstantin Belousov u_short color; 131286790a7SKonstantin Belousov 132b7ac5a85SKonstantin Belousov if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE) 133286790a7SKonstantin Belousov return (NULL); 134c42b43a0SKonstantin Belousov KASSERT(tp == OBJT_MGTDEVICE || ops->cdev_pg_populate == NULL, 135c42b43a0SKonstantin Belousov ("populate on unmanaged device pager")); 136df8bae1dSRodney W. Grimes 137df8bae1dSRodney W. Grimes /* 138df8bae1dSRodney W. Grimes * Offset should be page aligned. 139df8bae1dSRodney W. Grimes */ 140aa8de40aSPoul-Henning Kamp if (foff & PAGE_MASK) 141df8bae1dSRodney W. Grimes return (NULL); 142df8bae1dSRodney W. Grimes 143987ff181SKonstantin Belousov /* 144987ff181SKonstantin Belousov * Treat the mmap(2) file offset as an unsigned value for a 145987ff181SKonstantin Belousov * device mapping. This, in effect, allows a user to pass all 146987ff181SKonstantin Belousov * possible off_t values as the mapping cookie to the driver. At 147987ff181SKonstantin Belousov * this point, we know that both foff and size are a multiple 148987ff181SKonstantin Belousov * of the page size. Do a check to avoid wrap. 149987ff181SKonstantin Belousov */ 1506cde7a16SDavid Greenman size = round_page(size); 151987ff181SKonstantin Belousov pindex = UOFF_TO_IDX(foff) + UOFF_TO_IDX(size); 152987ff181SKonstantin Belousov if (pindex > OBJ_MAX_SIZE || pindex < UOFF_TO_IDX(foff) || 153987ff181SKonstantin Belousov pindex < UOFF_TO_IDX(size)) 154987ff181SKonstantin Belousov return (NULL); 1556cde7a16SDavid Greenman 156286790a7SKonstantin Belousov if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0) 157c8664f82SAlan Cox return (NULL); 158deea654eSKonstantin Belousov mtx_lock(&dev_pager_mtx); 15924a1cce3SDavid Greenman 16024a1cce3SDavid Greenman /* 161df8bae1dSRodney W. Grimes * Look up pager, creating as necessary. 162df8bae1dSRodney W. Grimes */ 163deea654eSKonstantin Belousov object1 = NULL; 16424a1cce3SDavid Greenman object = vm_pager_object_lookup(&dev_pager_object_list, handle); 16524a1cce3SDavid Greenman if (object == NULL) { 166df8bae1dSRodney W. Grimes /* 167e46cd413SAlan Cox * Allocate object and associate it with the pager. Initialize 168e46cd413SAlan Cox * the object's pg_color based upon the physical address of the 169e46cd413SAlan Cox * device's memory. 170df8bae1dSRodney W. Grimes */ 171deea654eSKonstantin Belousov mtx_unlock(&dev_pager_mtx); 172286790a7SKonstantin Belousov object1 = vm_object_allocate(tp, pindex); 173e46cd413SAlan Cox object1->flags |= OBJ_COLORED; 174286790a7SKonstantin Belousov object1->pg_color = color; 175286790a7SKonstantin Belousov object1->handle = handle; 176286790a7SKonstantin Belousov object1->un_pager.devp.ops = ops; 17743ab9660SKenneth D. Merry object1->un_pager.devp.dev = handle; 178339772b0SKonstantin Belousov TAILQ_INIT(&object1->un_pager.devp.devp_pglist); 179deea654eSKonstantin Belousov mtx_lock(&dev_pager_mtx); 180deea654eSKonstantin Belousov object = vm_pager_object_lookup(&dev_pager_object_list, handle); 181deea654eSKonstantin Belousov if (object != NULL) { 182deea654eSKonstantin Belousov /* 183deea654eSKonstantin Belousov * We raced with other thread while allocating object. 184deea654eSKonstantin Belousov */ 185deea654eSKonstantin Belousov if (pindex > object->size) 186deea654eSKonstantin Belousov object->size = pindex; 1878add0cedSJohn Baldwin KASSERT(object->type == tp, 1881be02479SKonstantin Belousov ("Inconsistent device pager type %p %d", 1891be02479SKonstantin Belousov object, tp)); 1901be02479SKonstantin Belousov KASSERT(object->un_pager.devp.ops == ops, 1911be02479SKonstantin Belousov ("Inconsistent devops %p %p", object, ops)); 192deea654eSKonstantin Belousov } else { 193deea654eSKonstantin Belousov object = object1; 194deea654eSKonstantin Belousov object1 = NULL; 19524a1cce3SDavid Greenman object->handle = handle; 196deea654eSKonstantin Belousov TAILQ_INSERT_TAIL(&dev_pager_object_list, object, 197deea654eSKonstantin Belousov pager_object_list); 198c42b43a0SKonstantin Belousov if (ops->cdev_pg_populate != NULL) 199c42b43a0SKonstantin Belousov vm_object_set_flag(object, OBJ_POPULATE); 200deea654eSKonstantin Belousov } 201df8bae1dSRodney W. Grimes } else { 2022f7af3dbSAlan Cox if (pindex > object->size) 2032f7af3dbSAlan Cox object->size = pindex; 2048add0cedSJohn Baldwin KASSERT(object->type == tp, 2058add0cedSJohn Baldwin ("Inconsistent device pager type %p %d", object, tp)); 206df8bae1dSRodney W. Grimes } 207deea654eSKonstantin Belousov mtx_unlock(&dev_pager_mtx); 208339772b0SKonstantin Belousov if (object1 != NULL) { 209339772b0SKonstantin Belousov object1->handle = object1; 210339772b0SKonstantin Belousov mtx_lock(&dev_pager_mtx); 211339772b0SKonstantin Belousov TAILQ_INSERT_TAIL(&dev_pager_object_list, object1, 212339772b0SKonstantin Belousov pager_object_list); 213339772b0SKonstantin Belousov mtx_unlock(&dev_pager_mtx); 214deea654eSKonstantin Belousov vm_object_deallocate(object1); 215339772b0SKonstantin Belousov } 21624a1cce3SDavid Greenman return (object); 21724a1cce3SDavid Greenman } 21824a1cce3SDavid Greenman 219286790a7SKonstantin Belousov static vm_object_t 220286790a7SKonstantin Belousov dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 221286790a7SKonstantin Belousov vm_ooffset_t foff, struct ucred *cred) 222286790a7SKonstantin Belousov { 223286790a7SKonstantin Belousov 224286790a7SKonstantin Belousov return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops, 225286790a7SKonstantin Belousov size, prot, foff, cred)); 226286790a7SKonstantin Belousov } 227286790a7SKonstantin Belousov 228286790a7SKonstantin Belousov void 229286790a7SKonstantin Belousov cdev_pager_free_page(vm_object_t object, vm_page_t m) 230286790a7SKonstantin Belousov { 231286790a7SKonstantin Belousov 23289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 233b7ac5a85SKonstantin Belousov if (object->type == OBJT_MGTDEVICE) { 234b7ac5a85SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m)); 235b7ac5a85SKonstantin Belousov pmap_remove_all(m); 236b7ac5a85SKonstantin Belousov vm_page_lock(m); 237b7ac5a85SKonstantin Belousov vm_page_remove(m); 238b7ac5a85SKonstantin Belousov vm_page_unlock(m); 239b7ac5a85SKonstantin Belousov } else if (object->type == OBJT_DEVICE) 240b7ac5a85SKonstantin Belousov dev_pager_free_page(object, m); 241b7ac5a85SKonstantin Belousov } 242b7ac5a85SKonstantin Belousov 243b7ac5a85SKonstantin Belousov static void 244b7ac5a85SKonstantin Belousov dev_pager_free_page(vm_object_t object, vm_page_t m) 245b7ac5a85SKonstantin Belousov { 246b7ac5a85SKonstantin Belousov 24789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 248b7ac5a85SKonstantin Belousov KASSERT((object->type == OBJT_DEVICE && 249b7ac5a85SKonstantin Belousov (m->oflags & VPO_UNMANAGED) != 0), 250b7ac5a85SKonstantin Belousov ("Managed device or page obj %p m %p", object, m)); 251c325e866SKonstantin Belousov TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q); 252286790a7SKonstantin Belousov vm_page_putfake(m); 253286790a7SKonstantin Belousov } 254286790a7SKonstantin Belousov 255f708ef1bSPoul-Henning Kamp static void 256b89def80SKonstantin Belousov dev_pager_dealloc(vm_object_t object) 25724a1cce3SDavid Greenman { 258df8bae1dSRodney W. Grimes vm_page_t m; 259df8bae1dSRodney W. Grimes 26089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 26143ab9660SKenneth D. Merry object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev); 262286790a7SKonstantin Belousov 263a9fa2c05SAlfred Perlstein mtx_lock(&dev_pager_mtx); 26424a1cce3SDavid Greenman TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 265a9fa2c05SAlfred Perlstein mtx_unlock(&dev_pager_mtx); 26689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 267b7ac5a85SKonstantin Belousov 268b7ac5a85SKonstantin Belousov if (object->type == OBJT_DEVICE) { 269df8bae1dSRodney W. Grimes /* 270df8bae1dSRodney W. Grimes * Free up our fake pages. 271df8bae1dSRodney W. Grimes */ 272b7ac5a85SKonstantin Belousov while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) 273b7ac5a85SKonstantin Belousov != NULL) 274b7ac5a85SKonstantin Belousov dev_pager_free_page(object, m); 275b7ac5a85SKonstantin Belousov } 276e735691bSJohn Baldwin object->handle = NULL; 277e735691bSJohn Baldwin object->type = OBJT_DEAD; 278df8bae1dSRodney W. Grimes } 279df8bae1dSRodney W. Grimes 280f708ef1bSPoul-Henning Kamp static int 281b0cd2017SGleb Smirnoff dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind, 282b0cd2017SGleb Smirnoff int *rahead) 283df8bae1dSRodney W. Grimes { 284b89def80SKonstantin Belousov int error; 285df8bae1dSRodney W. Grimes 286b0cd2017SGleb Smirnoff /* Since our haspage reports zero after/before, the count is 1. */ 287b0cd2017SGleb Smirnoff KASSERT(count == 1, ("%s: count %d", __func__, count)); 28889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 289c42b43a0SKonstantin Belousov if (object->un_pager.devp.ops->cdev_pg_fault == NULL) 290c42b43a0SKonstantin Belousov return (VM_PAGER_FAIL); 291286790a7SKonstantin Belousov error = object->un_pager.devp.ops->cdev_pg_fault(object, 292b0cd2017SGleb Smirnoff IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]); 293286790a7SKonstantin Belousov 29489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 295286790a7SKonstantin Belousov 296286790a7SKonstantin Belousov if (error == VM_PAGER_OK) { 297b7ac5a85SKonstantin Belousov KASSERT((object->type == OBJT_DEVICE && 298b0cd2017SGleb Smirnoff (ma[0]->oflags & VPO_UNMANAGED) != 0) || 299b7ac5a85SKonstantin Belousov (object->type == OBJT_MGTDEVICE && 300b0cd2017SGleb Smirnoff (ma[0]->oflags & VPO_UNMANAGED) == 0), 301b0cd2017SGleb Smirnoff ("Wrong page type %p %p", ma[0], object)); 302b7ac5a85SKonstantin Belousov if (object->type == OBJT_DEVICE) { 303286790a7SKonstantin Belousov TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, 304b0cd2017SGleb Smirnoff ma[0], plinks.q); 305286790a7SKonstantin Belousov } 306b0cd2017SGleb Smirnoff if (rbehind) 307b0cd2017SGleb Smirnoff *rbehind = 0; 308b0cd2017SGleb Smirnoff if (rahead) 309b0cd2017SGleb Smirnoff *rahead = 0; 310b7ac5a85SKonstantin Belousov } 311286790a7SKonstantin Belousov 312286790a7SKonstantin Belousov return (error); 313286790a7SKonstantin Belousov } 314286790a7SKonstantin Belousov 315286790a7SKonstantin Belousov static int 316c42b43a0SKonstantin Belousov dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type, 317c42b43a0SKonstantin Belousov vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last) 318c42b43a0SKonstantin Belousov { 319c42b43a0SKonstantin Belousov 320c42b43a0SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 321c42b43a0SKonstantin Belousov if (object->un_pager.devp.ops->cdev_pg_populate == NULL) 322c42b43a0SKonstantin Belousov return (VM_PAGER_FAIL); 323c42b43a0SKonstantin Belousov return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx, 324c42b43a0SKonstantin Belousov fault_type, max_prot, first, last)); 325c42b43a0SKonstantin Belousov } 326c42b43a0SKonstantin Belousov 327c42b43a0SKonstantin Belousov static int 328286790a7SKonstantin Belousov old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot, 329286790a7SKonstantin Belousov vm_page_t *mres) 330286790a7SKonstantin Belousov { 331286790a7SKonstantin Belousov vm_paddr_t paddr; 332286790a7SKonstantin Belousov vm_page_t m_paddr, page; 333286790a7SKonstantin Belousov struct cdev *dev; 334286790a7SKonstantin Belousov struct cdevsw *csw; 335286790a7SKonstantin Belousov struct file *fpop; 336286790a7SKonstantin Belousov struct thread *td; 337e9d37c9fSKonstantin Belousov vm_memattr_t memattr, memattr1; 338286790a7SKonstantin Belousov int ref, ret; 339286790a7SKonstantin Belousov 3403153e878SAlan Cox memattr = object->memattr; 341286790a7SKonstantin Belousov 34289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 343286790a7SKonstantin Belousov 344286790a7SKonstantin Belousov dev = object->handle; 3453979450bSKonstantin Belousov csw = dev_refthread(dev, &ref); 34691a1929fSAttilio Rao if (csw == NULL) { 34789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 34891a1929fSAttilio Rao return (VM_PAGER_FAIL); 34991a1929fSAttilio Rao } 3507818e0a5SKonstantin Belousov td = curthread; 3517818e0a5SKonstantin Belousov fpop = td->td_fpop; 3527818e0a5SKonstantin Belousov td->td_fpop = NULL; 353286790a7SKonstantin Belousov ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr); 3547818e0a5SKonstantin Belousov td->td_fpop = fpop; 3553979450bSKonstantin Belousov dev_relthread(dev, ref); 356286790a7SKonstantin Belousov if (ret != 0) { 357286790a7SKonstantin Belousov printf( 358286790a7SKonstantin Belousov "WARNING: dev_pager_getpage: map function returns error %d", ret); 35989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 360286790a7SKonstantin Belousov return (VM_PAGER_FAIL); 361286790a7SKonstantin Belousov } 362286790a7SKonstantin Belousov 3633153e878SAlan Cox /* If "paddr" is a real page, perform a sanity check on "memattr". */ 3643153e878SAlan Cox if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL && 365e9d37c9fSKonstantin Belousov (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) { 366e9d37c9fSKonstantin Belousov /* 367e9d37c9fSKonstantin Belousov * For the /dev/mem d_mmap routine to return the 368e9d37c9fSKonstantin Belousov * correct memattr, pmap_page_get_memattr() needs to 369e9d37c9fSKonstantin Belousov * be called, which we do there. 370e9d37c9fSKonstantin Belousov */ 371e9d37c9fSKonstantin Belousov if ((csw->d_flags & D_MEM) == 0) { 372e9d37c9fSKonstantin Belousov printf("WARNING: Device driver %s has set " 373e9d37c9fSKonstantin Belousov "\"memattr\" inconsistently (drv %u pmap %u).\n", 374e9d37c9fSKonstantin Belousov csw->d_name, memattr, memattr1); 375e9d37c9fSKonstantin Belousov } 376e9d37c9fSKonstantin Belousov memattr = memattr1; 3773153e878SAlan Cox } 378286790a7SKonstantin Belousov if (((*mres)->flags & PG_FICTITIOUS) != 0) { 37992bab635SDoug Rabson /* 380286790a7SKonstantin Belousov * If the passed in result page is a fake page, update it with 38192bab635SDoug Rabson * the new physical address. 38292bab635SDoug Rabson */ 383286790a7SKonstantin Belousov page = *mres; 38489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 38510cf2560SAlan Cox vm_page_updatefake(page, paddr, memattr); 38692bab635SDoug Rabson } else { 387df8bae1dSRodney W. Grimes /* 38807159f9cSMaxime Henrion * Replace the passed in reqpage page with our own fake page and 38907159f9cSMaxime Henrion * free up the all of the original pages. 390df8bae1dSRodney W. Grimes */ 39110cf2560SAlan Cox page = vm_page_getfake(paddr, memattr); 39289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 3938170d6e5SConrad Meyer vm_page_replace_checked(page, object, (*mres)->pindex, *mres); 394286790a7SKonstantin Belousov vm_page_lock(*mres); 395286790a7SKonstantin Belousov vm_page_free(*mres); 396286790a7SKonstantin Belousov vm_page_unlock(*mres); 397286790a7SKonstantin Belousov *mres = page; 39892bab635SDoug Rabson } 399ce1b857fSAlan Cox page->valid = VM_PAGE_BITS_ALL; 400df8bae1dSRodney W. Grimes return (VM_PAGER_OK); 401df8bae1dSRodney W. Grimes } 402df8bae1dSRodney W. Grimes 403e4542174SMatthew Dillon static void 404b89def80SKonstantin Belousov dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags, 405b89def80SKonstantin Belousov int *rtvals) 406df8bae1dSRodney W. Grimes { 407286790a7SKonstantin Belousov 408df8bae1dSRodney W. Grimes panic("dev_pager_putpage called"); 409df8bae1dSRodney W. Grimes } 410df8bae1dSRodney W. Grimes 411f708ef1bSPoul-Henning Kamp static boolean_t 412b89def80SKonstantin Belousov dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 413b89def80SKonstantin Belousov int *after) 414df8bae1dSRodney W. Grimes { 415b89def80SKonstantin Belousov 41624a1cce3SDavid Greenman if (before != NULL) 41724a1cce3SDavid Greenman *before = 0; 41824a1cce3SDavid Greenman if (after != NULL) 41924a1cce3SDavid Greenman *after = 0; 420df8bae1dSRodney W. Grimes return (TRUE); 421df8bae1dSRodney W. Grimes } 422286790a7SKonstantin Belousov 423286790a7SKonstantin Belousov static int 424286790a7SKonstantin Belousov old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 425286790a7SKonstantin Belousov vm_ooffset_t foff, struct ucred *cred, u_short *color) 426286790a7SKonstantin Belousov { 427286790a7SKonstantin Belousov struct cdev *dev; 428286790a7SKonstantin Belousov struct cdevsw *csw; 429286790a7SKonstantin Belousov vm_memattr_t dummy; 430286790a7SKonstantin Belousov vm_ooffset_t off; 431286790a7SKonstantin Belousov vm_paddr_t paddr; 432286790a7SKonstantin Belousov unsigned int npages; 433286790a7SKonstantin Belousov int ref; 434286790a7SKonstantin Belousov 435286790a7SKonstantin Belousov /* 436286790a7SKonstantin Belousov * Make sure this device can be mapped. 437286790a7SKonstantin Belousov */ 438286790a7SKonstantin Belousov dev = handle; 439286790a7SKonstantin Belousov csw = dev_refthread(dev, &ref); 440286790a7SKonstantin Belousov if (csw == NULL) 441286790a7SKonstantin Belousov return (ENXIO); 442286790a7SKonstantin Belousov 443286790a7SKonstantin Belousov /* 444286790a7SKonstantin Belousov * Check that the specified range of the device allows the desired 445286790a7SKonstantin Belousov * protection. 446286790a7SKonstantin Belousov * 447286790a7SKonstantin Belousov * XXX assumes VM_PROT_* == PROT_* 448286790a7SKonstantin Belousov */ 449286790a7SKonstantin Belousov npages = OFF_TO_IDX(size); 4507253a5ecSKonstantin Belousov paddr = 0; /* Make paddr initialized for the case of size == 0. */ 451286790a7SKonstantin Belousov for (off = foff; npages--; off += PAGE_SIZE) { 452286790a7SKonstantin Belousov if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) { 453286790a7SKonstantin Belousov dev_relthread(dev, ref); 454286790a7SKonstantin Belousov return (EINVAL); 455286790a7SKonstantin Belousov } 456286790a7SKonstantin Belousov } 457286790a7SKonstantin Belousov 458286790a7SKonstantin Belousov dev_ref(dev); 459286790a7SKonstantin Belousov dev_relthread(dev, ref); 460286790a7SKonstantin Belousov *color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE); 461286790a7SKonstantin Belousov return (0); 462286790a7SKonstantin Belousov } 463286790a7SKonstantin Belousov 464286790a7SKonstantin Belousov static void 465286790a7SKonstantin Belousov old_dev_pager_dtor(void *handle) 466286790a7SKonstantin Belousov { 467286790a7SKonstantin Belousov 468286790a7SKonstantin Belousov dev_rel(handle); 469286790a7SKonstantin Belousov } 470