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 * $FreeBSD$ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/conf.h> 45 #include <sys/mman.h> 46 #include <sys/malloc.h> 47 48 #include <vm/vm.h> 49 #include <vm/vm_object.h> 50 #include <vm/vm_page.h> 51 #include <vm/vm_pager.h> 52 #include <vm/vm_zone.h> 53 54 static void dev_pager_init __P((void)); 55 static vm_object_t dev_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t, 56 vm_ooffset_t)); 57 static void dev_pager_dealloc __P((vm_object_t)); 58 static int dev_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 59 static void dev_pager_putpages __P((vm_object_t, vm_page_t *, int, 60 boolean_t, int *)); 61 static boolean_t dev_pager_haspage __P((vm_object_t, vm_pindex_t, int *, 62 int *)); 63 64 /* list of device pager objects */ 65 static struct pagerlst dev_pager_object_list; 66 67 static vm_zone_t fakepg_zone; 68 static struct vm_zone fakepg_zone_store; 69 70 static vm_page_t dev_pager_getfake __P((vm_offset_t)); 71 static void dev_pager_putfake __P((vm_page_t)); 72 73 static int dev_pager_alloc_lock, dev_pager_alloc_lock_want; 74 75 struct pagerops devicepagerops = { 76 dev_pager_init, 77 dev_pager_alloc, 78 dev_pager_dealloc, 79 dev_pager_getpages, 80 dev_pager_putpages, 81 dev_pager_haspage, 82 NULL 83 }; 84 85 static void 86 dev_pager_init() 87 { 88 TAILQ_INIT(&dev_pager_object_list); 89 fakepg_zone = &fakepg_zone_store; 90 zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2); 91 } 92 93 static vm_object_t 94 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff) 95 { 96 dev_t dev; 97 d_mmap_t *mapfunc; 98 vm_object_t object; 99 unsigned int npages; 100 vm_offset_t off; 101 102 /* 103 * Make sure this device can be mapped. 104 */ 105 dev = handle; 106 mapfunc = devsw(dev)->d_mmap; 107 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) { 108 printf("obsolete map function %p\n", (void *)mapfunc); 109 return (NULL); 110 } 111 112 /* 113 * Offset should be page aligned. 114 */ 115 if (foff & PAGE_MASK) 116 return (NULL); 117 118 size = round_page(size); 119 120 /* 121 * Check that the specified range of the device allows the desired 122 * protection. 123 * 124 * XXX assumes VM_PROT_* == PROT_* 125 */ 126 npages = OFF_TO_IDX(size); 127 for (off = foff; npages--; off += PAGE_SIZE) 128 if ((*mapfunc) (dev, off, (int) prot) == -1) 129 return (NULL); 130 131 /* 132 * Lock to prevent object creation race contion. 133 */ 134 while (dev_pager_alloc_lock) { 135 dev_pager_alloc_lock_want++; 136 tsleep(&dev_pager_alloc_lock, PVM, "dvpall", 0); 137 dev_pager_alloc_lock_want--; 138 } 139 dev_pager_alloc_lock = 1; 140 141 /* 142 * Look up pager, creating as necessary. 143 */ 144 object = vm_pager_object_lookup(&dev_pager_object_list, handle); 145 if (object == NULL) { 146 /* 147 * Allocate object and associate it with the pager. 148 */ 149 object = vm_object_allocate(OBJT_DEVICE, 150 OFF_TO_IDX(foff + size)); 151 object->handle = handle; 152 TAILQ_INIT(&object->un_pager.devp.devp_pglist); 153 TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list); 154 } else { 155 /* 156 * Gain a reference to the object. 157 */ 158 vm_object_reference(object); 159 if (OFF_TO_IDX(foff + size) > object->size) 160 object->size = OFF_TO_IDX(foff + size); 161 } 162 163 dev_pager_alloc_lock = 0; 164 if (dev_pager_alloc_lock_want) 165 wakeup(&dev_pager_alloc_lock); 166 167 return (object); 168 } 169 170 static void 171 dev_pager_dealloc(object) 172 vm_object_t object; 173 { 174 vm_page_t m; 175 176 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 177 /* 178 * Free up our fake pages. 179 */ 180 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) { 181 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq); 182 dev_pager_putfake(m); 183 } 184 } 185 186 static int 187 dev_pager_getpages(object, m, count, reqpage) 188 vm_object_t object; 189 vm_page_t *m; 190 int count; 191 int reqpage; 192 { 193 vm_offset_t offset; 194 vm_offset_t paddr; 195 vm_page_t page; 196 dev_t dev; 197 int i, s; 198 d_mmap_t *mapfunc; 199 int prot; 200 201 dev = object->handle; 202 offset = m[reqpage]->pindex; 203 prot = PROT_READ; /* XXX should pass in? */ 204 mapfunc = devsw(dev)->d_mmap; 205 206 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) 207 panic("dev_pager_getpage: no map function"); 208 209 paddr = pmap_phys_address((*mapfunc) (dev, (vm_offset_t) offset << PAGE_SHIFT, prot)); 210 KASSERT(paddr != -1,("dev_pager_getpage: map function returns error")); 211 /* 212 * Replace the passed in reqpage page with our own fake page and free up the 213 * all of the original pages. 214 */ 215 page = dev_pager_getfake(paddr); 216 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq); 217 for (i = 0; i < count; i++) { 218 vm_page_free(m[i]); 219 } 220 s = splhigh(); 221 vm_page_insert(page, object, offset); 222 splx(s); 223 224 return (VM_PAGER_OK); 225 } 226 227 static void 228 dev_pager_putpages(object, m, count, sync, rtvals) 229 vm_object_t object; 230 vm_page_t *m; 231 int count; 232 boolean_t sync; 233 int *rtvals; 234 { 235 panic("dev_pager_putpage called"); 236 } 237 238 static boolean_t 239 dev_pager_haspage(object, pindex, before, after) 240 vm_object_t object; 241 vm_pindex_t pindex; 242 int *before; 243 int *after; 244 { 245 if (before != NULL) 246 *before = 0; 247 if (after != NULL) 248 *after = 0; 249 return (TRUE); 250 } 251 252 static vm_page_t 253 dev_pager_getfake(paddr) 254 vm_offset_t paddr; 255 { 256 vm_page_t m; 257 258 m = zalloc(fakepg_zone); 259 260 m->flags = PG_BUSY | PG_FICTITIOUS; 261 m->valid = VM_PAGE_BITS_ALL; 262 m->dirty = 0; 263 m->busy = 0; 264 m->queue = PQ_NONE; 265 m->object = NULL; 266 267 m->wire_count = 1; 268 m->hold_count = 0; 269 m->phys_addr = paddr; 270 271 return (m); 272 } 273 274 static void 275 dev_pager_putfake(m) 276 vm_page_t m; 277 { 278 if (!(m->flags & PG_FICTITIOUS)) 279 panic("dev_pager_putfake: bad page"); 280 zfree(fakepg_zone, m); 281 } 282