1 /*- 2 * Copyright (c) 2009 Advanced Computing Technologies LLC 3 * Written by: John H. Baldwin <jhb@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * This pager manages OBJT_SG objects. These objects are backed by 33 * a scatter/gather list of physical address ranges. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/sglist.h> 40 #include <vm/vm.h> 41 #include <vm/vm_object.h> 42 #include <vm/vm_page.h> 43 #include <vm/vm_pager.h> 44 #include <vm/uma.h> 45 46 static vm_object_t sg_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 47 vm_ooffset_t, struct ucred *); 48 static void sg_pager_dealloc(vm_object_t); 49 static int sg_pager_getpages(vm_object_t, vm_page_t *, int, int); 50 static void sg_pager_putpages(vm_object_t, vm_page_t *, int, 51 boolean_t, int *); 52 static boolean_t sg_pager_haspage(vm_object_t, vm_pindex_t, int *, 53 int *); 54 55 struct pagerops sgpagerops = { 56 .pgo_alloc = sg_pager_alloc, 57 .pgo_dealloc = sg_pager_dealloc, 58 .pgo_getpages = sg_pager_getpages, 59 .pgo_putpages = sg_pager_putpages, 60 .pgo_haspage = sg_pager_haspage, 61 }; 62 63 static vm_object_t 64 sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 65 vm_ooffset_t foff, struct ucred *cred) 66 { 67 struct sglist *sg; 68 vm_object_t object; 69 vm_pindex_t npages, pindex; 70 int i; 71 72 /* 73 * Offset should be page aligned. 74 */ 75 if (foff & PAGE_MASK) 76 return (NULL); 77 78 /* 79 * The scatter/gather list must only include page-aligned 80 * ranges. 81 */ 82 npages = 0; 83 sg = handle; 84 for (i = 0; i < sg->sg_nseg; i++) { 85 if ((sg->sg_segs[i].ss_paddr % PAGE_SIZE) != 0 || 86 (sg->sg_segs[i].ss_len % PAGE_SIZE) != 0) 87 return (NULL); 88 npages += sg->sg_segs[i].ss_len / PAGE_SIZE; 89 } 90 91 /* 92 * The scatter/gather list has a fixed size. Refuse requests 93 * to map beyond that. 94 */ 95 size = round_page(size); 96 pindex = OFF_TO_IDX(foff + size); 97 if (pindex > npages) 98 return (NULL); 99 100 /* 101 * Allocate a new object and associate it with the 102 * scatter/gather list. It is ok for our purposes to have 103 * multiple VM objects associated with the same scatter/gather 104 * list because scatter/gather lists are static. This is also 105 * simpler than ensuring a unique object per scatter/gather 106 * list. 107 */ 108 object = vm_object_allocate(OBJT_SG, npages); 109 object->handle = sglist_hold(sg); 110 TAILQ_INIT(&object->un_pager.sgp.sgp_pglist); 111 return (object); 112 } 113 114 static void 115 sg_pager_dealloc(vm_object_t object) 116 { 117 struct sglist *sg; 118 vm_page_t m; 119 120 /* 121 * Free up our fake pages. 122 */ 123 while ((m = TAILQ_FIRST(&object->un_pager.sgp.sgp_pglist)) != 0) { 124 TAILQ_REMOVE(&object->un_pager.sgp.sgp_pglist, m, pageq); 125 vm_page_putfake(m); 126 } 127 128 sg = object->handle; 129 sglist_free(sg); 130 } 131 132 static int 133 sg_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage) 134 { 135 struct sglist *sg; 136 vm_page_t m_paddr, page; 137 vm_pindex_t offset; 138 vm_paddr_t paddr; 139 vm_memattr_t memattr; 140 size_t space; 141 int i; 142 143 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 144 sg = object->handle; 145 memattr = object->memattr; 146 VM_OBJECT_UNLOCK(object); 147 offset = m[reqpage]->pindex; 148 149 /* 150 * Lookup the physical address of the requested page. An initial 151 * value of '1' instead of '0' is used so we can assert that the 152 * page is found since '0' can be a valid page-aligned physical 153 * address. 154 */ 155 space = 0; 156 paddr = 1; 157 for (i = 0; i < sg->sg_nseg; i++) { 158 if (space + sg->sg_segs[i].ss_len <= (offset * PAGE_SIZE)) { 159 space += sg->sg_segs[i].ss_len; 160 continue; 161 } 162 paddr = sg->sg_segs[i].ss_paddr + offset * PAGE_SIZE - space; 163 break; 164 } 165 KASSERT(paddr != 1, ("invalid SG page index")); 166 167 /* If "paddr" is a real page, perform a sanity check on "memattr". */ 168 if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL && 169 pmap_page_get_memattr(m_paddr) != memattr) { 170 memattr = pmap_page_get_memattr(m_paddr); 171 printf( 172 "WARNING: A device driver has set \"memattr\" inconsistently.\n"); 173 } 174 175 /* Return a fake page for the requested page. */ 176 KASSERT(!(m[reqpage]->flags & PG_FICTITIOUS), 177 ("backing page for SG is fake")); 178 179 /* Construct a new fake page. */ 180 page = vm_page_getfake(paddr, memattr); 181 VM_OBJECT_LOCK(object); 182 TAILQ_INSERT_TAIL(&object->un_pager.sgp.sgp_pglist, page, pageq); 183 184 /* Free the original pages and insert this fake page into the object. */ 185 for (i = 0; i < count; i++) { 186 vm_page_lock(m[i]); 187 vm_page_free(m[i]); 188 vm_page_unlock(m[i]); 189 } 190 vm_page_insert(page, object, offset); 191 m[reqpage] = page; 192 page->valid = VM_PAGE_BITS_ALL; 193 194 return (VM_PAGER_OK); 195 } 196 197 static void 198 sg_pager_putpages(vm_object_t object, vm_page_t *m, int count, 199 boolean_t sync, int *rtvals) 200 { 201 202 panic("sg_pager_putpage called"); 203 } 204 205 static boolean_t 206 sg_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 207 int *after) 208 { 209 210 if (before != NULL) 211 *before = 0; 212 if (after != NULL) 213 *after = 0; 214 return (TRUE); 215 } 216