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