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, 573364c323SKonstantin Belousov vm_ooffset_t, struct ucred *); 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 list manipulation */ 68a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx; 69a9fa2c05SAlfred Perlstein 70f708ef1bSPoul-Henning Kamp 71670d17b5SJeff Roberson static uma_zone_t fakepg_zone; 72df8bae1dSRodney W. Grimes 733153e878SAlan Cox static vm_page_t dev_pager_getfake(vm_paddr_t, vm_memattr_t); 7411caded3SAlfred Perlstein static void dev_pager_putfake(vm_page_t); 753153e878SAlan Cox static void dev_pager_updatefake(vm_page_t, vm_paddr_t, vm_memattr_t); 76df8bae1dSRodney W. Grimes 77df8bae1dSRodney W. Grimes struct pagerops devicepagerops = { 784e658600SPoul-Henning Kamp .pgo_init = dev_pager_init, 794e658600SPoul-Henning Kamp .pgo_alloc = dev_pager_alloc, 804e658600SPoul-Henning Kamp .pgo_dealloc = dev_pager_dealloc, 814e658600SPoul-Henning Kamp .pgo_getpages = dev_pager_getpages, 824e658600SPoul-Henning Kamp .pgo_putpages = dev_pager_putpages, 834e658600SPoul-Henning Kamp .pgo_haspage = dev_pager_haspage, 84df8bae1dSRodney W. Grimes }; 85df8bae1dSRodney W. Grimes 86f708ef1bSPoul-Henning Kamp static void 87df8bae1dSRodney W. Grimes dev_pager_init() 88df8bae1dSRodney W. Grimes { 8924a1cce3SDavid Greenman TAILQ_INIT(&dev_pager_object_list); 906008862bSJohn Baldwin mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF); 91670d17b5SJeff Roberson fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page), 92f3c625e4SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 93f3c625e4SJeff Roberson UMA_ZONE_NOFREE|UMA_ZONE_VM); 94df8bae1dSRodney W. Grimes } 95df8bae1dSRodney W. Grimes 96c8664f82SAlan Cox /* 97c8664f82SAlan Cox * MPSAFE 98c8664f82SAlan Cox */ 99f708ef1bSPoul-Henning Kamp static vm_object_t 1003364c323SKonstantin Belousov dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 1013364c323SKonstantin Belousov vm_ooffset_t foff, struct ucred *cred) 102df8bae1dSRodney W. Grimes { 10389c9c53dSPoul-Henning Kamp struct cdev *dev; 104deea654eSKonstantin Belousov vm_object_t object, object1; 1052f7af3dbSAlan Cox vm_pindex_t pindex; 1067095ee91SDoug Rabson unsigned int npages; 107227f9a1cSJake Burkholder vm_paddr_t paddr; 108cfd7baceSRobert Noland vm_ooffset_t off; 1092fa8c8d2SJohn Baldwin vm_memattr_t dummy; 110751fdd08SPoul-Henning Kamp struct cdevsw *csw; 111*3979450bSKonstantin Belousov int ref; 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; 126*3979450bSKonstantin Belousov csw = dev_refthread(dev, &ref); 127751fdd08SPoul-Henning Kamp if (csw == NULL) 128c8664f82SAlan Cox return (NULL); 129c8664f82SAlan Cox 130c8664f82SAlan Cox /* 1310d94caffSDavid Greenman * Check that the specified range of the device allows the desired 1320d94caffSDavid Greenman * protection. 133df8bae1dSRodney W. Grimes * 134df8bae1dSRodney W. Grimes * XXX assumes VM_PROT_* == PROT_* 135df8bae1dSRodney W. Grimes */ 1366cde7a16SDavid Greenman npages = OFF_TO_IDX(size); 137df8bae1dSRodney W. Grimes for (off = foff; npages--; off += PAGE_SIZE) 138cfd7baceSRobert Noland if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) { 139*3979450bSKonstantin Belousov dev_relthread(dev, ref); 140df8bae1dSRodney W. Grimes return (NULL); 141c8664f82SAlan Cox } 142df8bae1dSRodney W. Grimes 143deea654eSKonstantin Belousov mtx_lock(&dev_pager_mtx); 14424a1cce3SDavid Greenman 14524a1cce3SDavid Greenman /* 146df8bae1dSRodney W. Grimes * Look up pager, creating as necessary. 147df8bae1dSRodney W. Grimes */ 148deea654eSKonstantin Belousov object1 = NULL; 14924a1cce3SDavid Greenman object = vm_pager_object_lookup(&dev_pager_object_list, handle); 15024a1cce3SDavid Greenman if (object == NULL) { 151df8bae1dSRodney W. Grimes /* 152e46cd413SAlan Cox * Allocate object and associate it with the pager. Initialize 153e46cd413SAlan Cox * the object's pg_color based upon the physical address of the 154e46cd413SAlan Cox * device's memory. 155df8bae1dSRodney W. Grimes */ 156deea654eSKonstantin Belousov mtx_unlock(&dev_pager_mtx); 157deea654eSKonstantin Belousov object1 = vm_object_allocate(OBJT_DEVICE, pindex); 158e46cd413SAlan Cox object1->flags |= OBJ_COLORED; 159e46cd413SAlan Cox object1->pg_color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE); 160deea654eSKonstantin Belousov mtx_lock(&dev_pager_mtx); 161deea654eSKonstantin Belousov object = vm_pager_object_lookup(&dev_pager_object_list, handle); 162deea654eSKonstantin Belousov if (object != NULL) { 163deea654eSKonstantin Belousov /* 164deea654eSKonstantin Belousov * We raced with other thread while allocating object. 165deea654eSKonstantin Belousov */ 166deea654eSKonstantin Belousov if (pindex > object->size) 167deea654eSKonstantin Belousov object->size = pindex; 168deea654eSKonstantin Belousov } else { 169deea654eSKonstantin Belousov object = object1; 170deea654eSKonstantin Belousov object1 = NULL; 17124a1cce3SDavid Greenman object->handle = handle; 17224a1cce3SDavid Greenman TAILQ_INIT(&object->un_pager.devp.devp_pglist); 173deea654eSKonstantin Belousov TAILQ_INSERT_TAIL(&dev_pager_object_list, object, 174deea654eSKonstantin Belousov pager_object_list); 175deea654eSKonstantin Belousov } 176df8bae1dSRodney W. Grimes } else { 1772f7af3dbSAlan Cox if (pindex > object->size) 1782f7af3dbSAlan Cox object->size = pindex; 179df8bae1dSRodney W. Grimes } 180deea654eSKonstantin Belousov mtx_unlock(&dev_pager_mtx); 181*3979450bSKonstantin Belousov dev_relthread(dev, ref); 182deea654eSKonstantin Belousov vm_object_deallocate(object1); 18324a1cce3SDavid Greenman return (object); 18424a1cce3SDavid Greenman } 18524a1cce3SDavid Greenman 186f708ef1bSPoul-Henning Kamp static void 18724a1cce3SDavid Greenman dev_pager_dealloc(object) 188df8bae1dSRodney W. Grimes vm_object_t object; 18924a1cce3SDavid Greenman { 190df8bae1dSRodney W. Grimes vm_page_t m; 191df8bae1dSRodney W. Grimes 192deea654eSKonstantin Belousov VM_OBJECT_UNLOCK(object); 193a9fa2c05SAlfred Perlstein mtx_lock(&dev_pager_mtx); 19424a1cce3SDavid Greenman TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 195a9fa2c05SAlfred Perlstein mtx_unlock(&dev_pager_mtx); 196deea654eSKonstantin Belousov VM_OBJECT_LOCK(object); 197df8bae1dSRodney W. Grimes /* 198df8bae1dSRodney W. Grimes * Free up our fake pages. 199df8bae1dSRodney W. Grimes */ 200ce1b857fSAlan Cox while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != NULL) { 20124a1cce3SDavid Greenman TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq); 202df8bae1dSRodney W. Grimes dev_pager_putfake(m); 203df8bae1dSRodney W. Grimes } 204df8bae1dSRodney W. Grimes } 205df8bae1dSRodney W. Grimes 206f708ef1bSPoul-Henning Kamp static int 20724a1cce3SDavid Greenman dev_pager_getpages(object, m, count, reqpage) 20824a1cce3SDavid Greenman vm_object_t object; 20924a1cce3SDavid Greenman vm_page_t *m; 21024a1cce3SDavid Greenman int count; 21124a1cce3SDavid Greenman int reqpage; 212df8bae1dSRodney W. Grimes { 2136395da54SIan Dowse vm_pindex_t offset; 214227f9a1cSJake Burkholder vm_paddr_t paddr; 2153153e878SAlan Cox vm_page_t m_paddr, page; 2163153e878SAlan Cox vm_memattr_t memattr; 21789c9c53dSPoul-Henning Kamp struct cdev *dev; 218*3979450bSKonstantin Belousov int i, ref, ret; 219751fdd08SPoul-Henning Kamp struct cdevsw *csw; 2207818e0a5SKonstantin Belousov struct thread *td; 2217818e0a5SKonstantin Belousov struct file *fpop; 222df8bae1dSRodney W. Grimes 223a8ab4870SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 224ebb4a317SBruce Evans dev = object->handle; 225ce1b857fSAlan Cox page = m[reqpage]; 226ce1b857fSAlan Cox offset = page->pindex; 2273153e878SAlan Cox memattr = object->memattr; 2288630c117SAlan Cox VM_OBJECT_UNLOCK(object); 229*3979450bSKonstantin Belousov csw = dev_refthread(dev, &ref); 230751fdd08SPoul-Henning Kamp if (csw == NULL) 231751fdd08SPoul-Henning Kamp panic("dev_pager_getpage: no cdevsw"); 2327818e0a5SKonstantin Belousov td = curthread; 2337818e0a5SKonstantin Belousov fpop = td->td_fpop; 2347818e0a5SKonstantin Belousov td->td_fpop = NULL; 235cfd7baceSRobert Noland ret = csw->d_mmap(dev, (vm_ooffset_t)offset << PAGE_SHIFT, &paddr, 2362fa8c8d2SJohn Baldwin PROT_READ, &memattr); 23707159f9cSMaxime Henrion KASSERT(ret == 0, ("dev_pager_getpage: map function returns error")); 2387818e0a5SKonstantin Belousov td->td_fpop = fpop; 239*3979450bSKonstantin Belousov dev_relthread(dev, ref); 2403153e878SAlan Cox /* If "paddr" is a real page, perform a sanity check on "memattr". */ 2413153e878SAlan Cox if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL && 2423153e878SAlan Cox pmap_page_get_memattr(m_paddr) != memattr) { 2433153e878SAlan Cox memattr = pmap_page_get_memattr(m_paddr); 2443153e878SAlan Cox printf( 2453153e878SAlan Cox "WARNING: A device driver has set \"memattr\" inconsistently.\n"); 2463153e878SAlan Cox } 247ce1b857fSAlan Cox if ((page->flags & PG_FICTITIOUS) != 0) { 24892bab635SDoug Rabson /* 24992bab635SDoug Rabson * If the passed in reqpage page is a fake page, update it with 25092bab635SDoug Rabson * the new physical address. 25192bab635SDoug Rabson */ 25292bab635SDoug Rabson VM_OBJECT_LOCK(object); 2533153e878SAlan Cox dev_pager_updatefake(page, paddr, memattr); 25492bab635SDoug Rabson if (count > 1) { 2552965a453SKip Macy 25692bab635SDoug Rabson for (i = 0; i < count; i++) { 2572965a453SKip Macy if (i != reqpage) { 2582965a453SKip Macy vm_page_lock(m[i]); 25992bab635SDoug Rabson vm_page_free(m[i]); 2602965a453SKip Macy vm_page_unlock(m[i]); 2612965a453SKip Macy } 2622965a453SKip Macy } 26392bab635SDoug Rabson } 26492bab635SDoug Rabson } else { 265df8bae1dSRodney W. Grimes /* 26607159f9cSMaxime Henrion * Replace the passed in reqpage page with our own fake page and 26707159f9cSMaxime Henrion * free up the all of the original pages. 268df8bae1dSRodney W. Grimes */ 2693153e878SAlan Cox page = dev_pager_getfake(paddr, memattr); 2708630c117SAlan Cox VM_OBJECT_LOCK(object); 27124a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq); 2722965a453SKip Macy for (i = 0; i < count; i++) { 2732965a453SKip Macy vm_page_lock(m[i]); 27424a1cce3SDavid Greenman vm_page_free(m[i]); 2752965a453SKip Macy vm_page_unlock(m[i]); 2762965a453SKip Macy } 27726f9a767SRodney W. Grimes vm_page_insert(page, object, offset); 2789aa3d17dSAlan Cox m[reqpage] = page; 27992bab635SDoug Rabson } 280ce1b857fSAlan Cox page->valid = VM_PAGE_BITS_ALL; 281df8bae1dSRodney W. Grimes return (VM_PAGER_OK); 282df8bae1dSRodney W. Grimes } 283df8bae1dSRodney W. Grimes 284e4542174SMatthew Dillon static void 28524a1cce3SDavid Greenman dev_pager_putpages(object, m, count, sync, rtvals) 28624a1cce3SDavid Greenman vm_object_t object; 28724a1cce3SDavid Greenman vm_page_t *m; 28824a1cce3SDavid Greenman int count; 289df8bae1dSRodney W. Grimes boolean_t sync; 29024a1cce3SDavid Greenman int *rtvals; 291df8bae1dSRodney W. Grimes { 292df8bae1dSRodney W. Grimes panic("dev_pager_putpage called"); 293df8bae1dSRodney W. Grimes } 294df8bae1dSRodney W. Grimes 295f708ef1bSPoul-Henning Kamp static boolean_t 296a316d390SJohn Dyson dev_pager_haspage(object, pindex, before, after) 29724a1cce3SDavid Greenman vm_object_t object; 298a316d390SJohn Dyson vm_pindex_t pindex; 29924a1cce3SDavid Greenman int *before; 30024a1cce3SDavid Greenman int *after; 301df8bae1dSRodney W. Grimes { 30224a1cce3SDavid Greenman if (before != NULL) 30324a1cce3SDavid Greenman *before = 0; 30424a1cce3SDavid Greenman if (after != NULL) 30524a1cce3SDavid Greenman *after = 0; 306df8bae1dSRodney W. Grimes return (TRUE); 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes 30925f2e1c8SAlan Cox /* 3103153e878SAlan Cox * Create a fictitious page with the specified physical address and memory 3119861cbc6SAlan Cox * attribute. The memory attribute is the only the machine-dependent aspect 3129861cbc6SAlan Cox * of a fictitious page that must be initialized. 31325f2e1c8SAlan Cox */ 314df8bae1dSRodney W. Grimes static vm_page_t 3153153e878SAlan Cox dev_pager_getfake(vm_paddr_t paddr, vm_memattr_t memattr) 316df8bae1dSRodney W. Grimes { 317df8bae1dSRodney W. Grimes vm_page_t m; 318df8bae1dSRodney W. Grimes 3193153e878SAlan Cox m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO); 32026f9a767SRodney W. Grimes m->phys_addr = paddr; 3213153e878SAlan Cox /* Fictitious pages don't use "segind". */ 3223153e878SAlan Cox m->flags = PG_FICTITIOUS; 3233153e878SAlan Cox /* Fictitious pages don't use "order" or "pool". */ 3243153e878SAlan Cox m->oflags = VPO_BUSY; 3253153e878SAlan Cox m->wire_count = 1; 3263153e878SAlan Cox pmap_page_set_memattr(m, memattr); 327df8bae1dSRodney W. Grimes return (m); 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes 3303153e878SAlan Cox /* 3313153e878SAlan Cox * Release a fictitious page. 3323153e878SAlan Cox */ 333df8bae1dSRodney W. Grimes static void 3343153e878SAlan Cox dev_pager_putfake(vm_page_t m) 335df8bae1dSRodney W. Grimes { 3363153e878SAlan Cox 337df8bae1dSRodney W. Grimes if (!(m->flags & PG_FICTITIOUS)) 338df8bae1dSRodney W. Grimes panic("dev_pager_putfake: bad page"); 339670d17b5SJeff Roberson uma_zfree(fakepg_zone, m); 340df8bae1dSRodney W. Grimes } 34192bab635SDoug Rabson 3423153e878SAlan Cox /* 3433153e878SAlan Cox * Update the given fictitious page to the specified physical address and 3443153e878SAlan Cox * memory attribute. 3453153e878SAlan Cox */ 34692bab635SDoug Rabson static void 3473153e878SAlan Cox dev_pager_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 34892bab635SDoug Rabson { 3493153e878SAlan Cox 35092bab635SDoug Rabson if (!(m->flags & PG_FICTITIOUS)) 35192bab635SDoug Rabson panic("dev_pager_updatefake: bad page"); 35292bab635SDoug Rabson m->phys_addr = paddr; 3533153e878SAlan Cox pmap_page_set_memattr(m, memattr); 35492bab635SDoug Rabson } 355