1 /* 2 * Copyright (c) 1990 University of Utah. 3 * Copyright (c) 1991, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)device_pager.c 8.1 (Berkeley) 6/11/93 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/conf.h> 47 #include <sys/lock.h> 48 #include <sys/proc.h> 49 #include <sys/mutex.h> 50 #include <sys/mman.h> 51 #include <sys/sx.h> 52 53 #include <vm/vm.h> 54 #include <vm/vm_object.h> 55 #include <vm/vm_page.h> 56 #include <vm/vm_pager.h> 57 #include <vm/uma.h> 58 59 static void dev_pager_init(void); 60 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 61 vm_ooffset_t); 62 static void dev_pager_dealloc(vm_object_t); 63 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int); 64 static void dev_pager_putpages(vm_object_t, vm_page_t *, int, 65 boolean_t, int *); 66 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, 67 int *); 68 69 /* list of device pager objects */ 70 static struct pagerlst dev_pager_object_list; 71 /* protect against object creation */ 72 static struct sx dev_pager_sx; 73 /* protect list manipulation */ 74 static struct mtx dev_pager_mtx; 75 76 77 static uma_zone_t fakepg_zone; 78 79 static vm_page_t dev_pager_getfake(vm_paddr_t); 80 static void dev_pager_putfake(vm_page_t); 81 82 struct pagerops devicepagerops = { 83 .pgo_init = dev_pager_init, 84 .pgo_alloc = dev_pager_alloc, 85 .pgo_dealloc = dev_pager_dealloc, 86 .pgo_getpages = dev_pager_getpages, 87 .pgo_putpages = dev_pager_putpages, 88 .pgo_haspage = dev_pager_haspage, 89 }; 90 91 static void 92 dev_pager_init() 93 { 94 TAILQ_INIT(&dev_pager_object_list); 95 sx_init(&dev_pager_sx, "dev_pager create"); 96 mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF); 97 fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page), 98 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 99 } 100 101 /* 102 * MPSAFE 103 */ 104 static vm_object_t 105 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff) 106 { 107 dev_t dev; 108 d_mmap_t *mapfunc; 109 vm_object_t object; 110 unsigned int npages; 111 vm_paddr_t paddr; 112 vm_offset_t off; 113 114 /* 115 * Offset should be page aligned. 116 */ 117 if (foff & PAGE_MASK) 118 return (NULL); 119 120 size = round_page(size); 121 122 /* 123 * Make sure this device can be mapped. 124 */ 125 dev = handle; 126 mtx_lock(&Giant); 127 mapfunc = devsw(dev)->d_mmap; 128 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) { 129 printf("obsolete map function %p\n", (void *)mapfunc); 130 mtx_unlock(&Giant); 131 return (NULL); 132 } 133 134 /* 135 * Check that the specified range of the device allows the desired 136 * protection. 137 * 138 * XXX assumes VM_PROT_* == PROT_* 139 */ 140 npages = OFF_TO_IDX(size); 141 for (off = foff; npages--; off += PAGE_SIZE) 142 if ((*mapfunc)(dev, off, &paddr, (int)prot) != 0) { 143 mtx_unlock(&Giant); 144 return (NULL); 145 } 146 147 /* 148 * Lock to prevent object creation race condition. 149 */ 150 sx_xlock(&dev_pager_sx); 151 152 /* 153 * Look up pager, creating as necessary. 154 */ 155 object = vm_pager_object_lookup(&dev_pager_object_list, handle); 156 if (object == NULL) { 157 /* 158 * Allocate object and associate it with the pager. 159 */ 160 object = vm_object_allocate(OBJT_DEVICE, 161 OFF_TO_IDX(foff + size)); 162 object->handle = handle; 163 TAILQ_INIT(&object->un_pager.devp.devp_pglist); 164 mtx_lock(&dev_pager_mtx); 165 TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list); 166 mtx_unlock(&dev_pager_mtx); 167 } else { 168 /* 169 * Gain a reference to the object. 170 */ 171 vm_object_reference(object); 172 if (OFF_TO_IDX(foff + size) > object->size) 173 object->size = OFF_TO_IDX(foff + size); 174 } 175 176 sx_xunlock(&dev_pager_sx); 177 mtx_unlock(&Giant); 178 return (object); 179 } 180 181 static void 182 dev_pager_dealloc(object) 183 vm_object_t object; 184 { 185 vm_page_t m; 186 187 mtx_lock(&dev_pager_mtx); 188 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 189 mtx_unlock(&dev_pager_mtx); 190 /* 191 * Free up our fake pages. 192 */ 193 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) { 194 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq); 195 dev_pager_putfake(m); 196 } 197 } 198 199 static int 200 dev_pager_getpages(object, m, count, reqpage) 201 vm_object_t object; 202 vm_page_t *m; 203 int count; 204 int reqpage; 205 { 206 vm_pindex_t offset; 207 vm_paddr_t paddr; 208 vm_page_t page; 209 dev_t dev; 210 int i, ret; 211 d_mmap_t *mapfunc; 212 int prot; 213 214 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 215 dev = object->handle; 216 offset = m[reqpage]->pindex; 217 VM_OBJECT_UNLOCK(object); 218 prot = PROT_READ; /* XXX should pass in? */ 219 mapfunc = devsw(dev)->d_mmap; 220 221 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) 222 panic("dev_pager_getpage: no map function"); 223 224 ret = (*mapfunc)(dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr, prot); 225 KASSERT(ret == 0, ("dev_pager_getpage: map function returns error")); 226 /* 227 * Replace the passed in reqpage page with our own fake page and 228 * free up the all of the original pages. 229 */ 230 page = dev_pager_getfake(paddr); 231 VM_OBJECT_LOCK(object); 232 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq); 233 vm_page_lock_queues(); 234 for (i = 0; i < count; i++) 235 vm_page_free(m[i]); 236 vm_page_unlock_queues(); 237 vm_page_insert(page, object, offset); 238 239 return (VM_PAGER_OK); 240 } 241 242 static void 243 dev_pager_putpages(object, m, count, sync, rtvals) 244 vm_object_t object; 245 vm_page_t *m; 246 int count; 247 boolean_t sync; 248 int *rtvals; 249 { 250 panic("dev_pager_putpage called"); 251 } 252 253 static boolean_t 254 dev_pager_haspage(object, pindex, before, after) 255 vm_object_t object; 256 vm_pindex_t pindex; 257 int *before; 258 int *after; 259 { 260 if (before != NULL) 261 *before = 0; 262 if (after != NULL) 263 *after = 0; 264 return (TRUE); 265 } 266 267 static vm_page_t 268 dev_pager_getfake(paddr) 269 vm_paddr_t paddr; 270 { 271 vm_page_t m; 272 273 m = uma_zalloc(fakepg_zone, M_WAITOK); 274 275 m->flags = PG_BUSY | PG_FICTITIOUS; 276 m->valid = VM_PAGE_BITS_ALL; 277 m->dirty = 0; 278 m->busy = 0; 279 m->queue = PQ_NONE; 280 m->object = NULL; 281 282 m->wire_count = 1; 283 m->hold_count = 0; 284 m->phys_addr = paddr; 285 286 return (m); 287 } 288 289 static void 290 dev_pager_putfake(m) 291 vm_page_t m; 292 { 293 if (!(m->flags & PG_FICTITIOUS)) 294 panic("dev_pager_putfake: bad page"); 295 uma_zfree(fakepg_zone, m); 296 } 297