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, 99 UMA_ZONE_NOFREE|UMA_ZONE_VM); 100 } 101 102 /* 103 * MPSAFE 104 */ 105 static vm_object_t 106 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff) 107 { 108 dev_t dev; 109 d_mmap_t *mapfunc; 110 vm_object_t object; 111 vm_pindex_t pindex; 112 unsigned int npages; 113 vm_paddr_t paddr; 114 vm_offset_t off; 115 116 /* 117 * Offset should be page aligned. 118 */ 119 if (foff & PAGE_MASK) 120 return (NULL); 121 122 size = round_page(size); 123 pindex = OFF_TO_IDX(foff + size); 124 125 /* 126 * Make sure this device can be mapped. 127 */ 128 dev = handle; 129 mtx_lock(&Giant); 130 mapfunc = devsw(dev)->d_mmap; 131 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) { 132 printf("obsolete map function %p\n", (void *)mapfunc); 133 mtx_unlock(&Giant); 134 return (NULL); 135 } 136 137 /* 138 * Check that the specified range of the device allows the desired 139 * protection. 140 * 141 * XXX assumes VM_PROT_* == PROT_* 142 */ 143 npages = OFF_TO_IDX(size); 144 for (off = foff; npages--; off += PAGE_SIZE) 145 if ((*mapfunc)(dev, off, &paddr, (int)prot) != 0) { 146 mtx_unlock(&Giant); 147 return (NULL); 148 } 149 150 /* 151 * Lock to prevent object creation race condition. 152 */ 153 sx_xlock(&dev_pager_sx); 154 155 /* 156 * Look up pager, creating as necessary. 157 */ 158 object = vm_pager_object_lookup(&dev_pager_object_list, handle); 159 if (object == NULL) { 160 /* 161 * Allocate object and associate it with the pager. 162 */ 163 object = vm_object_allocate(OBJT_DEVICE, pindex); 164 object->handle = handle; 165 TAILQ_INIT(&object->un_pager.devp.devp_pglist); 166 mtx_lock(&dev_pager_mtx); 167 TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list); 168 mtx_unlock(&dev_pager_mtx); 169 } else { 170 /* 171 * Gain a reference to the object. 172 */ 173 vm_object_reference(object); 174 if (pindex > object->size) 175 object->size = pindex; 176 } 177 178 sx_xunlock(&dev_pager_sx); 179 mtx_unlock(&Giant); 180 return (object); 181 } 182 183 static void 184 dev_pager_dealloc(object) 185 vm_object_t object; 186 { 187 vm_page_t m; 188 189 mtx_lock(&dev_pager_mtx); 190 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 191 mtx_unlock(&dev_pager_mtx); 192 /* 193 * Free up our fake pages. 194 */ 195 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) { 196 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq); 197 dev_pager_putfake(m); 198 } 199 } 200 201 static int 202 dev_pager_getpages(object, m, count, reqpage) 203 vm_object_t object; 204 vm_page_t *m; 205 int count; 206 int reqpage; 207 { 208 vm_pindex_t offset; 209 vm_paddr_t paddr; 210 vm_page_t page; 211 dev_t dev; 212 int i, ret; 213 d_mmap_t *mapfunc; 214 int prot; 215 216 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 217 dev = object->handle; 218 offset = m[reqpage]->pindex; 219 VM_OBJECT_UNLOCK(object); 220 prot = PROT_READ; /* XXX should pass in? */ 221 mapfunc = devsw(dev)->d_mmap; 222 223 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) 224 panic("dev_pager_getpage: no map function"); 225 226 ret = (*mapfunc)(dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr, prot); 227 KASSERT(ret == 0, ("dev_pager_getpage: map function returns error")); 228 /* 229 * Replace the passed in reqpage page with our own fake page and 230 * free up the all of the original pages. 231 */ 232 page = dev_pager_getfake(paddr); 233 VM_OBJECT_LOCK(object); 234 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq); 235 vm_page_lock_queues(); 236 for (i = 0; i < count; i++) 237 vm_page_free(m[i]); 238 vm_page_unlock_queues(); 239 vm_page_insert(page, object, offset); 240 m[reqpage] = page; 241 242 return (VM_PAGER_OK); 243 } 244 245 static void 246 dev_pager_putpages(object, m, count, sync, rtvals) 247 vm_object_t object; 248 vm_page_t *m; 249 int count; 250 boolean_t sync; 251 int *rtvals; 252 { 253 panic("dev_pager_putpage called"); 254 } 255 256 static boolean_t 257 dev_pager_haspage(object, pindex, before, after) 258 vm_object_t object; 259 vm_pindex_t pindex; 260 int *before; 261 int *after; 262 { 263 if (before != NULL) 264 *before = 0; 265 if (after != NULL) 266 *after = 0; 267 return (TRUE); 268 } 269 270 static vm_page_t 271 dev_pager_getfake(paddr) 272 vm_paddr_t paddr; 273 { 274 vm_page_t m; 275 276 m = uma_zalloc(fakepg_zone, M_WAITOK); 277 278 m->flags = PG_BUSY | PG_FICTITIOUS; 279 m->valid = VM_PAGE_BITS_ALL; 280 m->dirty = 0; 281 m->busy = 0; 282 m->queue = PQ_NONE; 283 m->object = NULL; 284 285 m->wire_count = 1; 286 m->hold_count = 0; 287 m->phys_addr = paddr; 288 289 return (m); 290 } 291 292 static void 293 dev_pager_putfake(m) 294 vm_page_t m; 295 { 296 if (!(m->flags & PG_FICTITIOUS)) 297 panic("dev_pager_putfake: bad page"); 298 uma_zfree(fakepg_zone, m); 299 } 300